挺好用的socks5库go-socks5
1.挺好用的socks5库
2.示例代码
// Create a SOCKS5 server
conf := &socks5.Config{}
server, err := socks5.New(conf)
if err != nil {
panic(err)
}
// Create SOCKS5 proxy on localhost port 8000
if err := server.ListenAndServe("tcp", "127.0.0.1:8000"); err != nil {
panic(err)
}
很简洁,当然正式用不能这么用,默认是没有认证的,也就是说所有人都能连你服务器。。
加认证也很简单
cred := StaticCredentials{
"foo": "bar",
}
cator := UserPassAuthenticator{Credentials: cred}
s, _ := New(&Config{AuthMethods: []Authenticator{cator}})
下面详细看一下这个认证
Config结构体:
// Config is used to setup and configure a Server
type Config struct {
// AuthMethods can be provided to implement custom authentication
// By default, "auth-less" mode is enabled.
// For password-based auth use UserPassAuthenticator.
AuthMethods []Authenticator
// If provided, username/password authentication is enabled,
// by appending a UserPassAuthenticator to AuthMethods. If not provided,
// and AUthMethods is nil, then "auth-less" mode is enabled.
Credentials CredentialStore
// Resolver can be provided to do custom name resolution.
// Defaults to DNSResolver if not provided.
Resolver NameResolver
// Rules is provided to enable custom logic around permitting
// various commands. If not provided, PermitAll is used.
Rules RuleSet
// Rewriter can be used to transparently rewrite addresses.
// This is invoked before the RuleSet is invoked.
// Defaults to NoRewrite.
Rewriter AddressRewriter
// BindIP is used for bind or udp associate
BindIP net.IP
// Logger can be used to provide a custom log target.
// Defaults to stdout.
Logger *log.Logger
// Optional function for dialing out
Dial func(ctx context.Context, network, addr string) (net.Conn, error)
}
config结构体里面要传入AuthMethods
AuthMethods是一个Authenticator切片,Authenticator是个interface,
type Authenticator interface {
Authenticate(reader io.Reader, writer io.Writer) (*AuthContext, error)
GetCode() uint8
}
UserPassAuthenticator 实现了Authenticator
// UserPassAuthenticator is used to handle username/password based
// authentication
type UserPassAuthenticator struct {
Credentials CredentialStore
}
func (a UserPassAuthenticator) GetCode() uint8 {
return UserPassAuth
}
最后转化为填充一个UserPassAuthenticator的Credentials ,
而Credentials 是一个map[string]string
type StaticCredentials map[string]string
最后要把Config里面的logger 字段给填了(这里略),就差不多能用了。
3. 适用场景
适用移动网络访问github慢的场景。不知道为啥移动上github总是很慢。在公司电信网络就很快,之前家里用联通的时候也没这个现象,我上github你限制我干吗。。用此socks配合浏览器插件foxyProxy绕过移动网络直连github效果明显。注意foxyProxy里面设置Patterns ,不然什么请求到服务器上绕一圈可是有点过了。。