general: add some comments to functions

This commit is contained in:
nadoo 2017-09-10 20:33:35 +08:00
parent 53466d8183
commit 7ba0773fd7
17 changed files with 37 additions and 22 deletions

2
dns.go
View File

@ -62,7 +62,7 @@ type DNS struct {
answerHandlers []DNSAnswerHandler
}
// NewDNS returns a dns forwarder. client -> dns.udp -> glider -> forwarder -> remote dns addr
// NewDNS returns a dns forwarder. client[dns.udp] -> glider[tcp] -> forwarder[dns.tcp] -> remote dns addr
func NewDNS(addr, raddr string, sDialer Dialer) (*DNS, error) {
s := &DNS{
Forwarder: NewForwarder(addr, nil),

View File

@ -2,7 +2,7 @@
package main
// DNSTun .
// DNSTun struct
type DNSTun struct {
*Forwarder // as client
sDialer Dialer // dialer for server
@ -13,7 +13,7 @@ type DNSTun struct {
tcp *TCPTun
}
// NewDNSTun returns a dns forwarder.
// NewDNSTun returns a dns tunnel forwarder.
func NewDNSTun(addr, raddr string, sDialer Dialer) (*DNSTun, error) {
s := &DNSTun{
Forwarder: NewForwarder(addr, nil),

View File

@ -2,13 +2,13 @@ package main
import "net"
// Forwarder .
// Forwarder struct
type Forwarder struct {
addr string
cDialer Dialer
}
// NewForwarder .
// NewForwarder returns a base forwarder
func NewForwarder(addr string, cDialer Dialer) *Forwarder {
if cDialer == nil {
cDialer = Direct
@ -17,12 +17,15 @@ func NewForwarder(addr string, cDialer Dialer) *Forwarder {
return &Forwarder{addr: addr, cDialer: cDialer}
}
// Addr returns forwarder's address
func (p *Forwarder) Addr() string { return p.addr }
// Dial to remote addr via cDialer
func (p *Forwarder) Dial(network, addr string) (net.Conn, error) {
return p.cDialer.Dial(network, addr)
}
// NextDialer returns the next cDialer
func (p *Forwarder) NextDialer(dstAddr string) Dialer {
return p.cDialer
}

View File

@ -16,7 +16,7 @@ import (
"time"
)
// HTTP .
// HTTP struct
type HTTP struct {
*Forwarder // as client
sDialer Dialer // dialer for server

View File

@ -53,6 +53,7 @@ const NLA_F_NET_BYTEORDER = (1 << 14)
var nextSeqNr uint32
var nativeEndian binary.ByteOrder
// IPSetManager struct
type IPSetManager struct {
fd int
lsa syscall.SockaddrNetlink
@ -61,6 +62,7 @@ type IPSetManager struct {
domainSet sync.Map
}
// NewIPSetManager returns a IPSetManager
func NewIPSetManager(mainSet string, rules []*RuleConf) (*IPSetManager, error) {
fd, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW, syscall.NETLINK_NETFILTER)
if err != nil {
@ -111,7 +113,7 @@ func NewIPSetManager(mainSet string, rules []*RuleConf) (*IPSetManager, error) {
return m, nil
}
// AddDomainIP used to update ipset according to domainSet rule
// AddDomainIP implements the DNSAnswerHandler function, used to update ipset according to domainSet rule
func (m *IPSetManager) AddDomainIP(domain, ip string) error {
if ip != "" {

View File

@ -4,13 +4,15 @@ package main
import "errors"
type IPSetManager struct {
}
// IPSetManager struct
type IPSetManager struct{}
// NewIPSetManager returns a IPSetManager
func NewIPSetManager(mainSet string, rules []*RuleConf) (*IPSetManager, error) {
return nil, errors.New("ipset not supported on this os")
}
// AddDomainIP implements the DNSAnswerHandler function
func (m *IPSetManager) AddDomainIP(domain, ip string) error {
return errors.New("ipset not supported on this os")
}

2
log.go
View File

@ -2,7 +2,7 @@ package main
import "log"
// LogFunc .
// LogFunc defines a simple log function
type LogFunc func(f string, v ...interface{})
var logf LogFunc

View File

@ -17,7 +17,7 @@ var httpMethods = [...][]byte{
[]byte("TRACE"),
}
// MixedProxy .
// MixedProxy struct
type MixedProxy struct {
sDialer Dialer

View File

@ -5,7 +5,8 @@ import (
"unsafe"
)
const GETSOCKOPT = 15 // https://golang.org/src/syscall/syscall_linux_386.go#L183
// https://github.com/golang/go/blob/9e6b79a5dfb2f6fe4301ced956419a0da83bd025/src/syscall/syscall_linux_386.go#L196
const GETSOCKOPT = 15
func socketcall(call, a0, a1, a2, a3, a4, a5 uintptr) error {
var a [6]uintptr

View File

@ -7,7 +7,7 @@ import (
"log"
)
// RedirProxy .
// RedirProxy struct
type RedirProxy struct{}
// NewRedirProxy returns a redirect proxy.

View File

@ -7,7 +7,7 @@ import (
"sync"
)
// RuleDialer .
// RuleDialer struct
type RuleDialer struct {
gDialer Dialer
@ -16,7 +16,7 @@ type RuleDialer struct {
cidrMap sync.Map
}
// NewRuleDialer .
// NewRuleDialer returns a new rule dialer
func NewRuleDialer(rules []*RuleConf, gDialer Dialer) *RuleDialer {
rd := &RuleDialer{gDialer: gDialer}
@ -55,8 +55,10 @@ func NewRuleDialer(rules []*RuleConf, gDialer Dialer) *RuleDialer {
return rd
}
// Addr returns RuleDialer's address, always be "RULES"
func (rd *RuleDialer) Addr() string { return "RULES" }
// NextDialer return next dialer according to rule
func (p *RuleDialer) NextDialer(dstAddr string) Dialer {
// TODO: change to index finders
@ -106,6 +108,7 @@ func (p *RuleDialer) NextDialer(dstAddr string) Dialer {
return p.gDialer
}
// Dial dials to targer addr and return a conn
func (rd *RuleDialer) Dial(network, addr string) (net.Conn, error) {
return rd.NextDialer(addr).Dial(network, addr)
}

View File

@ -6,7 +6,7 @@ import (
"strings"
)
// Server .
// Server interface
type Server interface {
// ListenAndServe as proxy server, use only in server mode.
ListenAndServe()

View File

@ -55,7 +55,7 @@ var socks5Errors = []string{
"address type not supported",
}
// SOCKS5 .
// SOCKS5 struct
type SOCKS5 struct {
*Forwarder
sDialer Dialer

View File

@ -8,7 +8,7 @@ import (
"time"
)
// NewStrategyDialer .
// NewStrategyDialer returns a new Strategy Dialer
func NewStrategyDialer(strategy string, dialers []Dialer, website string, duration int) Dialer {
var dialer Dialer
if len(dialers) == 0 {
@ -32,7 +32,7 @@ func NewStrategyDialer(strategy string, dialers []Dialer, website string, durati
return dialer
}
// rrDialer
// rrDialer is the base struct of strategy dialer
type rrDialer struct {
dialers []Dialer
idx int
@ -44,7 +44,7 @@ type rrDialer struct {
duration int
}
// newRRDialer .
// newRRDialer returns a new rrDialer
func newRRDialer(dialers []Dialer, website string, duration int) *rrDialer {
rr := &rrDialer{dialers: dialers}

View File

@ -2,7 +2,7 @@ package main
import "net"
// TCPTun .
// TCPTun struct
type TCPTun struct {
*Forwarder
sDialer Dialer

View File

@ -7,6 +7,7 @@ import (
"syscall"
)
// TProxy struct
type TProxy struct {
*Forwarder // as client
sDialer Dialer // dialer for server
@ -28,10 +29,12 @@ func (s *TProxy) ListenAndServe() {
s.ListenAndServeUDP()
}
// ListenAndServeTCP .
func (s *TProxy) ListenAndServeTCP() {
logf("proxy-tproxy tcp mode not supported now, please use 'redir' instead")
}
// ListenAndServeUDP .
func (s *TProxy) ListenAndServeUDP() {
laddr, err := net.ResolveUDPAddr("udp", s.addr)
if err != nil {

View File

@ -7,6 +7,7 @@ import (
"log"
)
// TProxy struct
type TProxy struct{}
// NewTProxy returns a tproxy.