-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from vigo5190/auth
add auth
- Loading branch information
Showing
35 changed files
with
5,832 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"Vendor": true, | ||
"Deadline": "2m", | ||
"Sort": [ | ||
"linter", | ||
"severity", | ||
"path", | ||
"line" | ||
], | ||
"Exclude": [ | ||
"vendor" | ||
], | ||
"EnableGC": true, | ||
"Linters": { | ||
"nakedret": { | ||
"Command": "nakedret", | ||
"Pattern": "^(?P<path>.*?\\.go):(?P<line>\\d+)\\s*(?P<message>.*)$" | ||
} | ||
}, | ||
"WarnUnmatchedDirective": true, | ||
"DisableAll": true, | ||
"Enable": [ | ||
"deadcode", | ||
"gocyclo", | ||
"gofmt", | ||
"goimports", | ||
"golint", | ||
"gosimple", | ||
"ineffassign", | ||
"interfacer", | ||
"lll", | ||
"misspell", | ||
"nakedret", | ||
"unconvert", | ||
"unparam", | ||
"unused", | ||
"vet" | ||
], | ||
"Cyclo": 16, | ||
"LineLength": 100 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
```bash | ||
go test -bench=. -benchmem `go list ./... | grep -v vendor` | ||
? github.com/vigo5190/go-socks5 [no test files] | ||
goos: darwin | ||
goarch: amd64 | ||
pkg: github.com/vigo5190/go-socks5/proxy | ||
BenchmarkProxy_Start-8 10000 145070 ns/op 5130 B/op 66 allocs/op | ||
BenchmarkAuth_Auth-8 3000000 499 ns/op 96 B/op 3 allocs/op | ||
PASS | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
listen ="0.0.0.0:8008" | ||
|
||
auth = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
module github.com/vigo5190/go-socks5 | ||
|
||
require github.com/rs/zerolog v1.7.0 | ||
require ( | ||
github.com/BurntSushi/toml v0.3.0 | ||
github.com/rs/zerolog v1.7.0 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package proxy | ||
|
||
import ( | ||
"crypto/sha1" | ||
"encoding/base64" | ||
"hash" | ||
"sync" | ||
) | ||
|
||
//Authorizer interface for auth | ||
type Authorizer interface { | ||
AuthLoginPassword(login string, pass []byte) bool | ||
ShouldAuth() bool | ||
} | ||
|
||
//Auth container for auth info | ||
type Auth struct { | ||
AuthEnable bool | ||
Users map[string]string | ||
shapool sync.Pool | ||
} | ||
|
||
//ShouldAuth return true if aith enable | ||
func (p *Auth) ShouldAuth() bool { | ||
return p.AuthEnable | ||
} | ||
|
||
//AuthLoginPassword return true if auth enable and login/password corrected | ||
func (p *Auth) AuthLoginPassword(login string, pass []byte) bool { | ||
if !p.AuthEnable { | ||
return true | ||
} | ||
|
||
spass, ok := p.Users[login] | ||
|
||
if !ok { | ||
return false | ||
} | ||
|
||
hashedPass := p.hashSha(pass) | ||
|
||
return hashedPass == spass | ||
} | ||
|
||
func (p *Auth) hashSha(password []byte) string { | ||
s := p.shapool.Get() | ||
if s == nil { | ||
s = sha1.New() | ||
} | ||
|
||
ss := s.(hash.Hash) | ||
defer ss.Reset() | ||
defer p.shapool.Put(ss) | ||
|
||
ss.Write(password) | ||
passwordSum := ss.Sum(nil) | ||
|
||
return base64.StdEncoding.EncodeToString(passwordSum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package proxy | ||
|
||
import "testing" | ||
|
||
func TestProxy_Auth(t *testing.T) { | ||
p := Auth{ | ||
AuthEnable: true, | ||
Users: map[string]string{ | ||
"foo": "Ys23Ag/5IOWqZCw9QGaVDdHwH00=", | ||
}, | ||
} | ||
|
||
if !p.AuthLoginPassword("foo", []byte("bar")) { | ||
t.Error("expected auth true, got false") | ||
} | ||
|
||
if p.AuthLoginPassword("foo", []byte("bar1")) { | ||
t.Error("expected auth false, got true") | ||
} | ||
|
||
if p.AuthLoginPassword("fooq", []byte("bar")) { | ||
t.Error("expected auth false, got true") | ||
} | ||
} | ||
|
||
func TestProxy_Auth2(t *testing.T) { | ||
p := Auth{ | ||
AuthEnable: true, | ||
Users: map[string]string{ | ||
"foo": "Ys23Ag/5IOWqZCw9QGaVDdHwH00=", | ||
"bar": "C+7Hteo/D9vJXQ3UfzxbwnXaijM=", | ||
}, | ||
} | ||
|
||
if !p.AuthLoginPassword("foo", []byte("bar")) { | ||
t.Error("expected auth true, got false") | ||
} | ||
|
||
if p.AuthLoginPassword("foo", []byte("bar1")) { | ||
t.Error("expected auth false, got true") | ||
} | ||
|
||
if p.AuthLoginPassword("fooq", []byte("bar")) { | ||
t.Error("expected auth false, got true") | ||
} | ||
|
||
if !p.AuthLoginPassword("bar", []byte("foo")) { | ||
t.Error("expected auth true, got false") | ||
} | ||
|
||
if p.AuthLoginPassword("bar", []byte("foo1")) { | ||
t.Error("expected auth false, got true") | ||
} | ||
|
||
if p.AuthLoginPassword("bar1", []byte("foo")) { | ||
t.Error("expected auth false, got true") | ||
} | ||
} | ||
|
||
func BenchmarkAuth_Auth(b *testing.B) { | ||
p := Auth{ | ||
AuthEnable: true, | ||
Users: map[string]string{ | ||
"foo": "Ys23Ag/5IOWqZCw9QGaVDdHwH00=", | ||
}, | ||
} | ||
pass := []byte("bar") | ||
for n := 0; n < b.N; n++ { | ||
if !p.AuthLoginPassword("foo", pass) { | ||
b.Error("expected auth true, got false") | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.