-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathauth.go
74 lines (60 loc) · 1.96 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"net"
"github.com/Acebond/ReverseSocks5/statute"
)
// AuthContext A Request encapsulates authentication state provided
// during negotiation
type AuthContext struct {
// Provided auth method
Method uint8
// Payload provided during negotiation.
// Keys depend on the used auth method.
// For UserPass auth contains username/password
// Payload map[string]string
}
// Authenticator provide auth
type Authenticator interface {
Authenticate(conn net.Conn) error
GetCode() uint8
}
// NoAuthAuthenticator is used to handle the "No Authentication" mode
type NoAuthAuthenticator struct{}
// GetCode implement interface Authenticator
func (a NoAuthAuthenticator) GetCode() uint8 { return statute.MethodNoAuth }
// Authenticate implement interface Authenticator
func (a NoAuthAuthenticator) Authenticate(conn net.Conn) error {
_, err := conn.Write([]byte{statute.VersionSocks5, statute.MethodNoAuth})
return err
}
// UserPassAuthenticator is used to handle username/password based
// authentication
type UserPassAuthenticator struct {
Username string
Password string
}
// GetCode implement interface Authenticator
func (a UserPassAuthenticator) GetCode() uint8 { return statute.MethodUserPassAuth }
// Authenticate implement interface Authenticator
func (a UserPassAuthenticator) Authenticate(conn net.Conn) error {
// reply the client to use user/pass auth
if _, err := conn.Write([]byte{statute.VersionSocks5, statute.MethodUserPassAuth}); err != nil {
return err
}
// get user and user's password
nup, err := statute.ParseUserPassRequest(conn)
if err != nil {
return err
}
// Verify the password
if a.Username == string(nup.User) && a.Password == string(nup.Pass) {
if _, err = conn.Write([]byte{statute.UserPassAuthVersion, statute.AuthSuccess}); err != nil {
return err
}
return nil
}
if _, err := conn.Write([]byte{statute.UserPassAuthVersion, statute.AuthFailure}); err != nil {
return err
}
return statute.ErrUserAuthFailed
}