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

View File

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

4
log.go
View File

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

View File

@ -75,6 +75,7 @@ func ParseNode(s string) (node Node, err error) {
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 {
if !strings.Contains(addr, ":") {
addr = addr + ":80"

View File

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

View File

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

5
ssh.go
View File

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