| 1 | package socks5scip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/ |
| 2 | |
| 3 | import ( |
| 4 | "errorsscip-go gomod github.com/golang/go/src go1.16 errors/" |
| 5 | "ioscip-go gomod github.com/golang/go/src go1.16 io/" |
| 6 | "logscip-go gomod github.com/golang/go/src go1.16 log/" |
| 7 | ) |
| 8 | |
| 9 | var ( |
| 10 | // ErrBadReply is the error when read reply |
| 11 | ErrBadReplyscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/ErrBadReply. = errorsscip-go gomod github.com/golang/go/src go1.16 errors/.Newscip-go gomod github.com/golang/go/src go1.16 errors/New().("Bad Reply") |
| 12 | ) |
| 13 | |
| 14 | // NewNegotiationRequest return negotiation request packet can be writed into server |
| 15 | func NewNegotiationRequestscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NewNegotiationRequest().(methods []byte) *NegotiationRequestscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationRequest# { |
| 16 | return &NegotiationRequestscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationRequest#{ |
| 17 | Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationRequest#Ver.: Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Ver., |
| 18 | NMethodsscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationRequest#NMethods.: byte(len(methods)), |
| 19 | Methodsscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationRequest#Methods.: methods, |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | // WriteTo write negotiation request packet into server |
| 24 | func (r *NegotiationRequestscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationRequest#) WriteToscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationRequest#WriteTo().(w ioscip-go gomod github.com/golang/go/src go1.16 io/.Writerscip-go gomod github.com/golang/go/src go1.16 io/Writer#) (int64, error) { |
| 25 | i, err := w.Writescip-go gomod github.com/golang/go/src go1.16 io/Writer#Write().(append([]byte{r.Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationRequest#Ver., r.NMethodsscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationRequest#NMethods.}, r.Methodsscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationRequest#Methods....)) |
| 26 | if err != nil { |
| 27 | return 0, err |
| 28 | } |
| 29 | if Debugscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Debug. { |
| 30 | logscip-go gomod github.com/golang/go/src go1.16 log/.Printfscip-go gomod github.com/golang/go/src go1.16 log/Printf().("Sent NegotiationRequest: %#v %#v %#v\n", r.Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationRequest#Ver., r.NMethodsscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationRequest#NMethods., r.Methodsscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationRequest#Methods.) |
| 31 | } |
| 32 | return int64(i), nil |
| 33 | } |
| 34 | |
| 35 | // NewNegotiationReplyFrom read negotiation reply packet from server |
| 36 | func NewNegotiationReplyFromscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NewNegotiationReplyFrom().(r ioscip-go gomod github.com/golang/go/src go1.16 io/.Readerscip-go gomod github.com/golang/go/src go1.16 io/Reader#) (*NegotiationReplyscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationReply#, error) { |
| 37 | bb := make([]byte, 2) |
| 38 | if _, err := ioscip-go gomod github.com/golang/go/src go1.16 io/.ReadFullscip-go gomod github.com/golang/go/src go1.16 io/ReadFull().(r, bb); err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | if bb[0] != Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Ver. { |
| 42 | return nil, ErrVersionscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/ErrVersion. |
| 43 | } |
| 44 | if Debugscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Debug. { |
| 45 | logscip-go gomod github.com/golang/go/src go1.16 log/.Printfscip-go gomod github.com/golang/go/src go1.16 log/Printf().("Got NegotiationReply: %#v %#v\n", bb[0], bb[1]) |
| 46 | } |
| 47 | return &NegotiationReplyscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationReply#{ |
| 48 | Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationReply#Ver.: bb[0], |
| 49 | Methodscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NegotiationReply#Method.: bb[1], |
| 50 | }, nil |
| 51 | } |
| 52 | |
| 53 | // NewUserPassNegotiationRequest return user password negotiation request packet can be writed into server |
| 54 | func NewUserPassNegotiationRequestscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NewUserPassNegotiationRequest().(username []byte, password []byte) *UserPassNegotiationRequestscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest# { |
| 55 | return &UserPassNegotiationRequestscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#{ |
| 56 | Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Ver.: UserPassVerscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassVer., |
| 57 | Ulenscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Ulen.: byte(len(username)), |
| 58 | Unamescip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Uname.: username, |
| 59 | Plenscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Plen.: byte(len(password)), |
| 60 | Passwdscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Passwd.: password, |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // WriteTo write user password negotiation request packet into server |
| 65 | func (r *UserPassNegotiationRequestscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#) WriteToscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#WriteTo().(w ioscip-go gomod github.com/golang/go/src go1.16 io/.Writerscip-go gomod github.com/golang/go/src go1.16 io/Writer#) (int64, error) { |
| 66 | i, err := w.Writescip-go gomod github.com/golang/go/src go1.16 io/Writer#Write().(append(append(append([]byte{r.Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Ver., r.Ulenscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Ulen.}, r.Unamescip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Uname....), r.Plenscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Plen.), r.Passwdscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Passwd....)) |
| 67 | if err != nil { |
| 68 | return 0, err |
| 69 | } |
| 70 | if Debugscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Debug. { |
| 71 | logscip-go gomod github.com/golang/go/src go1.16 log/.Printfscip-go gomod github.com/golang/go/src go1.16 log/Printf().("Sent UserNameNegotiationRequest: %#v %#v %#v %#v %#v\n", r.Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Ver., r.Ulenscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Ulen., r.Unamescip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Uname., r.Plenscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Plen., r.Passwdscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationRequest#Passwd.) |
| 72 | } |
| 73 | return int64(i), nil |
| 74 | } |
| 75 | |
| 76 | // NewUserPassNegotiationReplyFrom read user password negotiation reply packet from server |
| 77 | func NewUserPassNegotiationReplyFromscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NewUserPassNegotiationReplyFrom().(r ioscip-go gomod github.com/golang/go/src go1.16 io/.Readerscip-go gomod github.com/golang/go/src go1.16 io/Reader#) (*UserPassNegotiationReplyscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationReply#, error) { |
| 78 | bb := make([]byte, 2) |
| 79 | if _, err := ioscip-go gomod github.com/golang/go/src go1.16 io/.ReadFullscip-go gomod github.com/golang/go/src go1.16 io/ReadFull().(r, bb); err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | if bb[0] != UserPassVerscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassVer. { |
| 83 | return nil, ErrUserPassVersionscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/ErrUserPassVersion. |
| 84 | } |
| 85 | if Debugscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Debug. { |
| 86 | logscip-go gomod github.com/golang/go/src go1.16 log/.Printfscip-go gomod github.com/golang/go/src go1.16 log/Printf().("Got UserPassNegotiationReply: %#v %#v \n", bb[0], bb[1]) |
| 87 | } |
| 88 | return &UserPassNegotiationReplyscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationReply#{ |
| 89 | Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationReply#Ver.: bb[0], |
| 90 | Statusscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/UserPassNegotiationReply#Status.: bb[1], |
| 91 | }, nil |
| 92 | } |
| 93 | |
| 94 | // NewRequest return request packet can be writed into server, dstaddr should not have domain length |
| 95 | func NewRequestscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NewRequest().(cmd byte, atyp byte, dstaddr []byte, dstport []byte) *Requestscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request# { |
| 96 | if atyp == ATYPDomainscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/ATYPDomain. { |
| 97 | dstaddr = append([]byte{byte(len(dstaddr))}, dstaddr...) |
| 98 | } |
| 99 | return &Requestscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#{ |
| 100 | Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#Ver.: Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Ver., |
| 101 | Cmdscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#Cmd.: cmd, |
| 102 | Rsvscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#Rsv.: 0x00, |
| 103 | Atypscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#Atyp.: atyp, |
| 104 | DstAddrscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#DstAddr.: dstaddr, |
| 105 | DstPortscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#DstPort.: dstport, |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // WriteTo write request packet into server |
| 110 | func (r *Requestscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#) WriteToscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#WriteTo().(w ioscip-go gomod github.com/golang/go/src go1.16 io/.Writerscip-go gomod github.com/golang/go/src go1.16 io/Writer#) (int64, error) { |
| 111 | i, err := w.Writescip-go gomod github.com/golang/go/src go1.16 io/Writer#Write().(append(append([]byte{r.Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#Ver., r.Cmdscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#Cmd., r.Rsvscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#Rsv., r.Atypscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#Atyp.}, r.DstAddrscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#DstAddr....), r.DstPortscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#DstPort....)) |
| 112 | if err != nil { |
| 113 | return 0, err |
| 114 | } |
| 115 | if Debugscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Debug. { |
| 116 | logscip-go gomod github.com/golang/go/src go1.16 log/.Printfscip-go gomod github.com/golang/go/src go1.16 log/Printf().("Sent Request: %#v %#v %#v %#v %#v %#v\n", r.Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#Ver., r.Cmdscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#Cmd., r.Rsvscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#Rsv., r.Atypscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#Atyp., r.DstAddrscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#DstAddr., r.DstPortscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Request#DstPort.) |
| 117 | } |
| 118 | return int64(i), nil |
| 119 | } |
| 120 | |
| 121 | // NewReplyFrom read reply packet from server |
| 122 | func NewReplyFromscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/NewReplyFrom().(r ioscip-go gomod github.com/golang/go/src go1.16 io/.Readerscip-go gomod github.com/golang/go/src go1.16 io/Reader#) (*Replyscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Reply#, error) { |
| 123 | bb := make([]byte, 4) |
| 124 | if _, err := ioscip-go gomod github.com/golang/go/src go1.16 io/.ReadFullscip-go gomod github.com/golang/go/src go1.16 io/ReadFull().(r, bb); err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 | if bb[0] != Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Ver. { |
| 128 | return nil, ErrVersionscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/ErrVersion. |
| 129 | } |
| 130 | var addr []byte |
| 131 | if bb[3] == ATYPIPv4scip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/ATYPIPv4. { |
| 132 | addr = make([]byte, 4) |
| 133 | if _, err := ioscip-go gomod github.com/golang/go/src go1.16 io/.ReadFullscip-go gomod github.com/golang/go/src go1.16 io/ReadFull().(r, addr); err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | } else if bb[3] == ATYPIPv6scip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/ATYPIPv6. { |
| 137 | addr = make([]byte, 16) |
| 138 | if _, err := ioscip-go gomod github.com/golang/go/src go1.16 io/.ReadFullscip-go gomod github.com/golang/go/src go1.16 io/ReadFull().(r, addr); err != nil { |
| 139 | return nil, err |
| 140 | } |
| 141 | } else if bb[3] == ATYPDomainscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/ATYPDomain. { |
| 142 | dal := make([]byte, 1) |
| 143 | if _, err := ioscip-go gomod github.com/golang/go/src go1.16 io/.ReadFullscip-go gomod github.com/golang/go/src go1.16 io/ReadFull().(r, dal); err != nil { |
| 144 | return nil, err |
| 145 | } |
| 146 | if dal[0] == 0 { |
| 147 | return nil, ErrBadReplyscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/ErrBadReply. |
| 148 | } |
| 149 | addr = make([]byte, int(dal[0])) |
| 150 | if _, err := ioscip-go gomod github.com/golang/go/src go1.16 io/.ReadFullscip-go gomod github.com/golang/go/src go1.16 io/ReadFull().(r, addr); err != nil { |
| 151 | return nil, err |
| 152 | } |
| 153 | addr = append(dal, addr...) |
| 154 | } else { |
| 155 | return nil, ErrBadReplyscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/ErrBadReply. |
| 156 | } |
| 157 | port := make([]byte, 2) |
| 158 | if _, err := ioscip-go gomod github.com/golang/go/src go1.16 io/.ReadFullscip-go gomod github.com/golang/go/src go1.16 io/ReadFull().(r, port); err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | if Debugscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Debug. { |
| 162 | logscip-go gomod github.com/golang/go/src go1.16 log/.Printfscip-go gomod github.com/golang/go/src go1.16 log/Printf().("Got Reply: %#v %#v %#v %#v %#v %#v\n", bb[0], bb[1], bb[2], bb[3], addr, port) |
| 163 | } |
| 164 | return &Replyscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Reply#{ |
| 165 | Verscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Reply#Ver.: bb[0], |
| 166 | Repscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Reply#Rep.: bb[1], |
| 167 | Rsvscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Reply#Rsv.: bb[2], |
| 168 | Atypscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Reply#Atyp.: bb[3], |
| 169 | BndAddrscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Reply#BndAddr.: addr, |
| 170 | BndPortscip-go gomod github.com/txthinking/socks5 . `github.com/txthinking/socks5`/Reply#BndPort.: port, |
| 171 | }, nil |
| 172 | } |