add comment, fix golint

This commit is contained in:
rui.zheng 2017-08-12 19:55:35 +08:00
parent fb713ae73c
commit 4d5fa98857
8 changed files with 78 additions and 34 deletions

View File

@ -112,18 +112,21 @@ type DialOptions struct {
// DialOption allows a common way to set dial options. // DialOption allows a common way to set dial options.
type DialOption func(opts *DialOptions) type DialOption func(opts *DialOptions)
// TimeoutDialOption specifies the timeout used by Transporter.Dial
func TimeoutDialOption(timeout time.Duration) DialOption { func TimeoutDialOption(timeout time.Duration) DialOption {
return func(opts *DialOptions) { return func(opts *DialOptions) {
opts.Timeout = timeout opts.Timeout = timeout
} }
} }
// ChainDialOption specifies a chain used by Transporter.Dial
func ChainDialOption(chain *Chain) DialOption { func ChainDialOption(chain *Chain) DialOption {
return func(opts *DialOptions) { return func(opts *DialOptions) {
opts.Chain = chain opts.Chain = chain
} }
} }
// IPDialOption specifies an IP list used by Transporter.Dial
func IPDialOption(ips ...string) DialOption { func IPDialOption(ips ...string) DialOption {
return func(opts *DialOptions) { return func(opts *DialOptions) {
opts.IPs = ips opts.IPs = ips
@ -146,54 +149,63 @@ type HandshakeOptions struct {
// HandshakeOption allows a common way to set handshake options. // HandshakeOption allows a common way to set handshake options.
type HandshakeOption func(opts *HandshakeOptions) type HandshakeOption func(opts *HandshakeOptions)
// AddrHandshakeOption specifies the server address
func AddrHandshakeOption(addr string) HandshakeOption { func AddrHandshakeOption(addr string) HandshakeOption {
return func(opts *HandshakeOptions) { return func(opts *HandshakeOptions) {
opts.Addr = addr opts.Addr = addr
} }
} }
// UserHandshakeOption specifies the user used by Transporter.Handshake
func UserHandshakeOption(user *url.Userinfo) HandshakeOption { func UserHandshakeOption(user *url.Userinfo) HandshakeOption {
return func(opts *HandshakeOptions) { return func(opts *HandshakeOptions) {
opts.User = user opts.User = user
} }
} }
// TimeoutHandshakeOption specifies the timeout used by Transporter.Handshake
func TimeoutHandshakeOption(timeout time.Duration) HandshakeOption { func TimeoutHandshakeOption(timeout time.Duration) HandshakeOption {
return func(opts *HandshakeOptions) { return func(opts *HandshakeOptions) {
opts.Timeout = timeout opts.Timeout = timeout
} }
} }
// IntervalHandshakeOption specifies the interval time used by Transporter.Handshake
func IntervalHandshakeOption(interval time.Duration) HandshakeOption { func IntervalHandshakeOption(interval time.Duration) HandshakeOption {
return func(opts *HandshakeOptions) { return func(opts *HandshakeOptions) {
opts.Interval = interval opts.Interval = interval
} }
} }
// RetryHandshakeOption specifies the times of retry used by Transporter.Handshake
func RetryHandshakeOption(retry int) HandshakeOption { func RetryHandshakeOption(retry int) HandshakeOption {
return func(opts *HandshakeOptions) { return func(opts *HandshakeOptions) {
opts.Retry = retry opts.Retry = retry
} }
} }
// TLSConfigHandshakeOption specifies the TLS config used by Transporter.Handshake
func TLSConfigHandshakeOption(config *tls.Config) HandshakeOption { func TLSConfigHandshakeOption(config *tls.Config) HandshakeOption {
return func(opts *HandshakeOptions) { return func(opts *HandshakeOptions) {
opts.TLSConfig = config opts.TLSConfig = config
} }
} }
// WSOptionsHandshakeOption specifies the websocket options used by websocket handshake
func WSOptionsHandshakeOption(options *WSOptions) HandshakeOption { func WSOptionsHandshakeOption(options *WSOptions) HandshakeOption {
return func(opts *HandshakeOptions) { return func(opts *HandshakeOptions) {
opts.WSOptions = options opts.WSOptions = options
} }
} }
// KCPConfigHandshakeOption specifies the KCP config used by KCP handshake
func KCPConfigHandshakeOption(config *KCPConfig) HandshakeOption { func KCPConfigHandshakeOption(config *KCPConfig) HandshakeOption {
return func(opts *HandshakeOptions) { return func(opts *HandshakeOptions) {
opts.KCPConfig = config opts.KCPConfig = config
} }
} }
// QUICConfigHandshakeOption specifies the QUIC config used by QUIC handshake
func QUICConfigHandshakeOption(config *QUICConfig) HandshakeOption { func QUICConfigHandshakeOption(config *QUICConfig) HandshakeOption {
return func(opts *HandshakeOptions) { return func(opts *HandshakeOptions) {
opts.QUICConfig = config opts.QUICConfig = config

View File

@ -44,7 +44,10 @@ var (
) )
var ( var (
// DefaultTLSConfig is a default TLS config for internal use
DefaultTLSConfig *tls.Config DefaultTLSConfig *tls.Config
// DefaultUserAgent is the default HTTP User-Agent header used by HTTP and websocket
DefaultUserAgent = "Chrome/60.0.3112.90" DefaultUserAgent = "Chrome/60.0.3112.90"
) )
@ -64,6 +67,7 @@ func init() {
log.DefaultLogger = &LogLogger{} log.DefaultLogger = &LogLogger{}
} }
// SetLogger sets a new logger for internal log system
func SetLogger(logger log.Logger) { func SetLogger(logger log.Logger) {
log.DefaultLogger = logger log.DefaultLogger = logger
} }

4
log.go
View File

@ -13,10 +13,12 @@ func init() {
type LogLogger struct { type LogLogger struct {
} }
// Log uses the standard log library log.Output
func (l *LogLogger) Log(v ...interface{}) { func (l *LogLogger) Log(v ...interface{}) {
log.Output(3, fmt.Sprintln(v...)) log.Output(3, fmt.Sprintln(v...))
} }
// Logf uses the standard log library log.Output
func (l *LogLogger) Logf(format string, v ...interface{}) { func (l *LogLogger) Logf(format string, v ...interface{}) {
log.Output(3, fmt.Sprintf(format, v...)) log.Output(3, fmt.Sprintf(format, v...))
} }
@ -25,8 +27,10 @@ func (l *LogLogger) Logf(format string, v ...interface{}) {
type NopLogger struct { type NopLogger struct {
} }
// Log does nothing
func (l *NopLogger) Log(v ...interface{}) { func (l *NopLogger) Log(v ...interface{}) {
} }
// Logf does nothing
func (l *NopLogger) Logf(format string, v ...interface{}) { func (l *NopLogger) Logf(format string, v ...interface{}) {
} }

View File

@ -75,6 +75,7 @@ func ParseNode(s string) (node Node, err error) {
return return
} }
// Can tests whether the given action and address is allowed by the whitelist and blacklist.
func Can(action string, addr string, whitelist, blacklist *Permissions) bool { func Can(action string, addr string, whitelist, blacklist *Permissions) bool {
if !strings.Contains(addr, ":") { if !strings.Contains(addr, ":") {
addr = addr + ":80" addr = addr + ":80"

View File

@ -23,6 +23,7 @@ type obfs4Context struct {
var obfs4Map = make(map[string]obfs4Context) var obfs4Map = make(map[string]obfs4Context)
// Obfs4Init initializes the obfs client or server based on isServeNode
func Obfs4Init(node Node, isServeNode bool) error { func Obfs4Init(node Node, isServeNode bool) error {
if _, ok := obfs4Map[node.Addr]; ok { if _, ok := obfs4Map[node.Addr]; ok {
return fmt.Errorf("obfs4 context already inited") return fmt.Errorf("obfs4 context already inited")

View File

@ -9,36 +9,20 @@ import (
glob "github.com/ryanuber/go-glob" glob "github.com/ryanuber/go-glob"
) )
// Permission is a rule for blacklist and whitelist.
type Permission struct { type Permission struct {
Actions StringSet Actions StringSet
Hosts StringSet Hosts StringSet
Ports PortSet Ports PortSet
} }
type Permissions []Permission // PortRange specifies the range of port, such as 1000-2000.
func minint(x, y int) int {
if x < y {
return x
}
return y
}
func maxint(x, y int) int {
if x > y {
return x
}
return y
}
type PortRange struct { type PortRange struct {
Min, Max int Min, Max int
} }
func (ir *PortRange) Contains(value int) bool { // ParsePortRange parses the s to a PortRange.
return value >= ir.Min && value <= ir.Max // The s may be a '*' means 0-65535.
}
func ParsePortRange(s string) (*PortRange, error) { func ParsePortRange(s string) (*PortRange, error) {
if s == "*" { if s == "*" {
return &PortRange{Min: 0, Max: 65535}, nil return &PortRange{Min: 0, Max: 65535}, nil
@ -74,18 +58,16 @@ func ParsePortRange(s string) (*PortRange, error) {
} }
} }
func (ps *PortSet) Contains(value int) bool { // Contains checks whether the value is within this range.
for _, portRange := range *ps { func (ir *PortRange) Contains(value int) bool {
if portRange.Contains(value) { return value >= ir.Min && value <= ir.Max
return true
}
}
return false
} }
// PortSet is a set of PortRange
type PortSet []PortRange type PortSet []PortRange
// ParsePortSet parses the s to a PortSet.
// The s shoud be a comma separated string.
func ParsePortSet(s string) (*PortSet, error) { func ParsePortSet(s string) (*PortSet, error) {
ps := &PortSet{} ps := &PortSet{}
@ -108,9 +90,10 @@ func ParsePortSet(s string) (*PortSet, error) {
return ps, nil return ps, nil
} }
func (ss *StringSet) Contains(subj string) bool { // Contains checks whether the value is within this port set.
for _, s := range *ss { func (ps *PortSet) Contains(value int) bool {
if glob.Glob(s, subj) { for _, portRange := range *ps {
if portRange.Contains(value) {
return true return true
} }
} }
@ -118,8 +101,11 @@ func (ss *StringSet) Contains(subj string) bool {
return false return false
} }
// StringSet is a set of string.
type StringSet []string type StringSet []string
// ParseStringSet parses the s to a StringSet.
// The s shoud be a comma separated string.
func ParseStringSet(s string) (*StringSet, error) { func ParseStringSet(s string) (*StringSet, error) {
ss := &StringSet{} ss := &StringSet{}
if s == "" { if s == "" {
@ -131,9 +117,10 @@ func ParseStringSet(s string) (*StringSet, error) {
return ss, nil return ss, nil
} }
func (ps *Permissions) Can(action string, host string, port int) bool { // Contains checks whether the string subj within this StringSet.
for _, p := range *ps { func (ss *StringSet) Contains(subj string) bool {
if p.Actions.Contains(action) && p.Hosts.Contains(host) && p.Ports.Contains(port) { for _, s := range *ss {
if glob.Glob(s, subj) {
return true return true
} }
} }
@ -141,6 +128,10 @@ func (ps *Permissions) Can(action string, host string, port int) bool {
return false return false
} }
// Permissions is a set of Permission.
type Permissions []Permission
// ParsePermissions parses the s to a Permissions.
func ParsePermissions(s string) (*Permissions, error) { func ParsePermissions(s string) (*Permissions, error) {
ps := &Permissions{} ps := &Permissions{}
@ -183,3 +174,28 @@ func ParsePermissions(s string) (*Permissions, error) {
return ps, nil return ps, nil
} }
// Can tests whether the given action and host:port is allowed by this Permissions.
func (ps *Permissions) Can(action string, host string, port int) bool {
for _, p := range *ps {
if p.Actions.Contains(action) && p.Hosts.Contains(host) && p.Ports.Contains(port) {
return true
}
}
return false
}
func minint(x, y int) int {
if x < y {
return x
}
return y
}
func maxint(x, y int) int {
if x > y {
return x
}
return y
}

View File

@ -131,6 +131,7 @@ func (tr *quicTransporter) Multiplex() bool {
return true return true
} }
// QUICConfig is the config for QUIC client and server
type QUICConfig struct { type QUICConfig struct {
TLSConfig *tls.Config TLSConfig *tls.Config
Timeout time.Duration Timeout time.Duration

5
ssh.go
View File

@ -34,6 +34,7 @@ var (
type sshDirectForwardConnector struct { type sshDirectForwardConnector struct {
} }
// SSHDirectForwardConnector creates a Connector for SSH TCP direct port forwarding.
func SSHDirectForwardConnector() Connector { func SSHDirectForwardConnector() Connector {
return &sshDirectForwardConnector{} return &sshDirectForwardConnector{}
} }
@ -54,6 +55,7 @@ func (c *sshDirectForwardConnector) Connect(conn net.Conn, raddr string) (net.Co
type sshRemoteForwardConnector struct { type sshRemoteForwardConnector struct {
} }
// SSHRemoteForwardConnector creates a Connector for SSH TCP remote port forwarding.
func SSHRemoteForwardConnector() Connector { func SSHRemoteForwardConnector() Connector {
return &sshRemoteForwardConnector{} return &sshRemoteForwardConnector{}
} }
@ -108,6 +110,7 @@ type sshForwardTransporter struct {
sessionMutex sync.Mutex sessionMutex sync.Mutex
} }
// SSHForwardTransporter creates a Transporter that is used by SSH port forwarding server.
func SSHForwardTransporter() Transporter { func SSHForwardTransporter() Transporter {
return &sshForwardTransporter{ return &sshForwardTransporter{
sessions: make(map[string]*sshSession), sessions: make(map[string]*sshSession),
@ -406,6 +409,7 @@ type sshForwardHandler struct {
config *ssh.ServerConfig config *ssh.ServerConfig
} }
// SSHForwardHandler creates a server Handler for SSH port forwarding server.
func SSHForwardHandler(opts ...HandlerOption) Handler { func SSHForwardHandler(opts ...HandlerOption) Handler {
h := &sshForwardHandler{ h := &sshForwardHandler{
options: new(HandlerOptions), options: new(HandlerOptions),
@ -744,6 +748,7 @@ func getHostPortFromAddr(addr net.Addr) (host string, port int, err error) {
return return
} }
// PasswordCallbackFunc is a callback function used by SSH server.
type PasswordCallbackFunc func(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) type PasswordCallbackFunc func(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error)
func defaultSSHPasswordCallback(users ...*url.Userinfo) PasswordCallbackFunc { func defaultSSHPasswordCallback(users ...*url.Userinfo) PasswordCallbackFunc {