style: rename vars to avoid namespace pollution

This commit is contained in:
KujouRinka 2024-01-27 11:17:39 +08:00
parent 90542be7f2
commit ddfb2ce2af

View File

@ -10,20 +10,20 @@ import (
const ( const (
Socks5Version = 0x05 Socks5Version = 0x05
CmdTCPConnect = 0x01 Socks5CmdTCPConnect = 0x01
CmdTCPBind = 0x02 Socks5CmdTCPBind = 0x02
CmdUDPAssociate = 0x03 Socks5CmdUDPAssociate = 0x03
AuthNotRequired = 0x00 Socks5AuthNotRequired = 0x00
AuthPassword = 0x02 Socks5AuthPassword = 0x02
AuthNoMatchingMethod = 0xFF Socks5AuthNoMatchingMethod = 0xFF
AuthSuccess = 0x00 Socks5AuthSuccess = 0x00
AuthFailure = 0x01 Socks5AuthFailure = 0x01
AddrTypeIPv4 = 0x01 Socks5AddrTypeIPv4 = 0x01
AddrTypeDomain = 0x03 Socks5AddrTypeDomain = 0x03
AddrTypeIPv6 = 0x04 Socks5AddrTypeIPv6 = 0x04
) )
var _ analyzer.Analyzer = (*Socks5Analyzer)(nil) var _ analyzer.Analyzer = (*Socks5Analyzer)(nil)
@ -40,7 +40,7 @@ func (a *Socks5Analyzer) Limit() int {
} }
func (a *Socks5Analyzer) NewTCP(info analyzer.TCPInfo, logger analyzer.Logger) analyzer.TCPStream { func (a *Socks5Analyzer) NewTCP(info analyzer.TCPInfo, logger analyzer.Logger) analyzer.TCPStream {
return newSocksStream(logger) return newSocks5Stream(logger)
} }
type socks5Stream struct { type socks5Stream struct {
@ -65,7 +65,7 @@ type socks5Stream struct {
authRespMethod int authRespMethod int
} }
func newSocksStream(logger analyzer.Logger) *socks5Stream { func newSocks5Stream(logger analyzer.Logger) *socks5Stream {
s := &socks5Stream{logger: logger, reqBuf: &utils.ByteBuffer{}, respBuf: &utils.ByteBuffer{}} s := &socks5Stream{logger: logger, reqBuf: &utils.ByteBuffer{}, respBuf: &utils.ByteBuffer{}}
s.reqLSM = utils.NewLinearStateMachine( s.reqLSM = utils.NewLinearStateMachine(
s.parseSocks5ReqVersion, s.parseSocks5ReqVersion,
@ -146,14 +146,14 @@ func (s *socks5Stream) parseSocks5ReqMethod() utils.LSMAction {
} }
// For convenience, we only take the first method we can process // For convenience, we only take the first method we can process
s.authReqMethod = AuthNoMatchingMethod s.authReqMethod = Socks5AuthNoMatchingMethod
for _, method := range methods[1:] { for _, method := range methods[1:] {
switch method { switch method {
case AuthNotRequired: case Socks5AuthNotRequired:
s.authReqMethod = AuthNotRequired s.authReqMethod = Socks5AuthNotRequired
break break
case AuthPassword: case Socks5AuthPassword:
s.authReqMethod = AuthPassword s.authReqMethod = Socks5AuthPassword
break break
default: default:
// TODO: more auth method to support // TODO: more auth method to support
@ -165,9 +165,9 @@ func (s *socks5Stream) parseSocks5ReqMethod() utils.LSMAction {
func (s *socks5Stream) parseSocks5ReqAuth() utils.LSMAction { func (s *socks5Stream) parseSocks5ReqAuth() utils.LSMAction {
switch s.authReqMethod { switch s.authReqMethod {
case AuthNotRequired: case Socks5AuthNotRequired:
s.reqMap["auth"] = analyzer.PropMap{"method": s.authReqMethod} s.reqMap["auth"] = analyzer.PropMap{"method": s.authReqMethod}
case AuthPassword: case Socks5AuthPassword:
meta, ok := s.reqBuf.Get(2, false) meta, ok := s.reqBuf.Get(2, false)
if !ok { if !ok {
return utils.LSMActionPause return utils.LSMActionPause
@ -211,18 +211,18 @@ func (s *socks5Stream) parseSocks5ReqConnInfo() utils.LSMAction {
} }
// verify socks version // verify socks version
if preInfo[0] != 0x05 { if preInfo[0] != Socks5Version {
return utils.LSMActionCancel return utils.LSMActionCancel
} }
var pktLen int var pktLen int
switch int(preInfo[3]) { switch int(preInfo[3]) {
case AddrTypeIPv4: case Socks5AddrTypeIPv4:
pktLen = 10 pktLen = 10
case AddrTypeDomain: case Socks5AddrTypeDomain:
domainLen := int(preInfo[4]) domainLen := int(preInfo[4])
pktLen = 7 + domainLen pktLen = 7 + domainLen
case AddrTypeIPv6: case Socks5AddrTypeIPv6:
pktLen = 22 pktLen = 22
default: default:
return utils.LSMActionCancel return utils.LSMActionCancel
@ -235,7 +235,7 @@ func (s *socks5Stream) parseSocks5ReqConnInfo() utils.LSMAction {
// parse cmd // parse cmd
cmd := int(pkt[1]) cmd := int(pkt[1])
if cmd != CmdTCPConnect && cmd != CmdTCPBind && cmd != CmdUDPAssociate { if cmd != Socks5CmdTCPConnect && cmd != Socks5CmdTCPBind && cmd != Socks5CmdUDPAssociate {
return utils.LSMActionCancel return utils.LSMActionCancel
} }
s.reqMap["cmd"] = cmd s.reqMap["cmd"] = cmd
@ -244,11 +244,11 @@ func (s *socks5Stream) parseSocks5ReqConnInfo() utils.LSMAction {
addrType := int(pkt[3]) addrType := int(pkt[3])
var addr string var addr string
switch addrType { switch addrType {
case AddrTypeIPv4: case Socks5AddrTypeIPv4:
addr = net.IPv4(pkt[4], pkt[5], pkt[6], pkt[7]).String() addr = net.IPv4(pkt[4], pkt[5], pkt[6], pkt[7]).String()
case AddrTypeDomain: case Socks5AddrTypeDomain:
addr = string(pkt[5 : 5+pkt[4]]) addr = string(pkt[5 : 5+pkt[4]])
case AddrTypeIPv6: case Socks5AddrTypeIPv6:
addr = net.IP(pkt[4 : 4+net.IPv6len]).String() addr = net.IP(pkt[4 : 4+net.IPv6len]).String()
default: default:
return utils.LSMActionCancel return utils.LSMActionCancel
@ -278,9 +278,9 @@ func (s *socks5Stream) parseSocks5RespVerAndMethod() utils.LSMAction {
func (s *socks5Stream) parseSocks5RespAuth() utils.LSMAction { func (s *socks5Stream) parseSocks5RespAuth() utils.LSMAction {
switch s.authRespMethod { switch s.authRespMethod {
case AuthNotRequired: case Socks5AuthNotRequired:
s.respMap["auth"] = analyzer.PropMap{"method": s.authRespMethod} s.respMap["auth"] = analyzer.PropMap{"method": s.authRespMethod}
case AuthPassword: case Socks5AuthPassword:
authResp, ok := s.respBuf.Get(2, true) authResp, ok := s.respBuf.Get(2, true)
if !ok { if !ok {
return utils.LSMActionPause return utils.LSMActionPause
@ -318,12 +318,12 @@ func (s *socks5Stream) parseSocks5RespConnInfo() utils.LSMAction {
var pktLen int var pktLen int
switch int(preInfo[3]) { switch int(preInfo[3]) {
case AddrTypeIPv4: case Socks5AddrTypeIPv4:
pktLen = 10 pktLen = 10
case AddrTypeDomain: case Socks5AddrTypeDomain:
domainLen := int(preInfo[4]) domainLen := int(preInfo[4])
pktLen = 7 + domainLen pktLen = 7 + domainLen
case AddrTypeIPv6: case Socks5AddrTypeIPv6:
pktLen = 22 pktLen = 22
default: default:
return utils.LSMActionCancel return utils.LSMActionCancel
@ -342,11 +342,11 @@ func (s *socks5Stream) parseSocks5RespConnInfo() utils.LSMAction {
addrType := int(pkt[3]) addrType := int(pkt[3])
var addr string var addr string
switch addrType { switch addrType {
case AddrTypeIPv4: case Socks5AddrTypeIPv4:
addr = net.IPv4(pkt[4], pkt[5], pkt[6], pkt[7]).String() addr = net.IPv4(pkt[4], pkt[5], pkt[6], pkt[7]).String()
case AddrTypeDomain: case Socks5AddrTypeDomain:
addr = string(pkt[5 : 5+pkt[4]]) addr = string(pkt[5 : 5+pkt[4]])
case AddrTypeIPv6: case Socks5AddrTypeIPv6:
addr = net.IP(pkt[4 : 4+net.IPv6len]).String() addr = net.IP(pkt[4 : 4+net.IPv6len]).String()
default: default:
return utils.LSMActionCancel return utils.LSMActionCancel