| 1 | ## socks5 |
| 2 | |
| 3 | [English](README.md) |
| 4 | |
| 5 | [](https://goreportcard.com/report/github.com/txthinking/socks5) |
| 6 | [](https://godoc.org/github.com/txthinking/socks5) |
| 7 | |
| 8 | [🗣 News](https://t.me/s/txthinking_news) |
| 9 | [🩸 Youtube](https://www.youtube.com/txthinking) |
| 10 | |
| 11 | SOCKS Protocol Version 5 Library. |
| 12 | |
| 13 | 完整 TCP/UDP 和 IPv4/IPv6 支持. |
| 14 | 目标: KISS, less is more, small API, code is like the original protocol. |
| 15 | |
| 16 | ❤️ A project by [txthinking.com](https://www.txthinking.com) |
| 17 | |
| 18 | ### 获取 |
| 19 | ``` |
| 20 | $ go get github.com/txthinking/socks5 |
| 21 | ``` |
| 22 | |
| 23 | ### Struct的概念 对标 原始协议里的概念 |
| 24 | |
| 25 | * Negotiation: |
| 26 | * `type NegotiationRequest struct` |
| 27 | * `func NewNegotiationRequest(methods []byte)`, in client |
| 28 | * `func (r *NegotiationRequest) WriteTo(w io.Writer)`, client writes to server |
| 29 | * `func NewNegotiationRequestFrom(r io.Reader)`, server reads from client |
| 30 | * `type NegotiationReply struct` |
| 31 | * `func NewNegotiationReply(method byte)`, in server |
| 32 | * `func (r *NegotiationReply) WriteTo(w io.Writer)`, server writes to client |
| 33 | * `func NewNegotiationReplyFrom(r io.Reader)`, client reads from server |
| 34 | * User and password negotiation: |
| 35 | * `type UserPassNegotiationRequest struct` |
| 36 | * `func NewUserPassNegotiationRequest(username []byte, password []byte)`, in client |
| 37 | * `func (r *UserPassNegotiationRequest) WriteTo(w io.Writer)`, client writes to server |
| 38 | * `func NewUserPassNegotiationRequestFrom(r io.Reader)`, server reads from client |
| 39 | * `type UserPassNegotiationReply struct` |
| 40 | * `func NewUserPassNegotiationReply(status byte)`, in server |
| 41 | * `func (r *UserPassNegotiationReply) WriteTo(w io.Writer)`, server writes to client |
| 42 | * `func NewUserPassNegotiationReplyFrom(r io.Reader)`, client reads from server |
| 43 | * Request: |
| 44 | * `type Request struct` |
| 45 | * `func NewRequest(cmd byte, atyp byte, dstaddr []byte, dstport []byte)`, in client |
| 46 | * `func (r *Request) WriteTo(w io.Writer)`, client writes to server |
| 47 | * `func NewRequestFrom(r io.Reader)`, server reads from client |
| 48 | * After server gets the client's *Request, processes... |
| 49 | * Reply: |
| 50 | * `type Reply struct` |
| 51 | * `func NewReply(rep byte, atyp byte, bndaddr []byte, bndport []byte)`, in server |
| 52 | * `func (r *Reply) WriteTo(w io.Writer)`, server writes to client |
| 53 | * `func NewReplyFrom(r io.Reader)`, client reads from server |
| 54 | * Datagram: |
| 55 | * `type Datagram struct` |
| 56 | * `func NewDatagram(atyp byte, dstaddr []byte, dstport []byte, data []byte)` |
| 57 | * `func NewDatagramFromBytes(bb []byte)` |
| 58 | * `func (d *Datagram) Bytes()` |
| 59 | |
| 60 | ### 高级 API |
| 61 | |
| 62 | > 这可以满足经典场景,特殊场景推荐你选择上面的小API来自定义。 |
| 63 | |
| 64 | **Server**: 支持UDP和TCP |
| 65 | |
| 66 | * `type Server struct` |
| 67 | * `type Handler interface` |
| 68 | * `TCPHandle(*Server, *net.TCPConn, *Request) error` |
| 69 | * `UDPHandle(*Server, *net.UDPAddr, *Datagram) error` |
| 70 | |
| 71 | 举例: |
| 72 | |
| 73 | ``` |
| 74 | server, _ := NewClassicServer(addr, ip, username, password, tcpTimeout, udpTimeout) |
| 75 | server.ListenAndServe(Handler) |
| 76 | ``` |
| 77 | |
| 78 | **Client**: 支持TCP和UDP, 返回net.Conn |
| 79 | |
| 80 | * `type Client struct` |
| 81 | |
| 82 | 举例: |
| 83 | |
| 84 | ``` |
| 85 | client, _ := socks5.NewClient(server, username, password, tcpTimeout, udpTimeout) |
| 86 | conn, _ := client.Dial(network, addr) |
| 87 | ``` |
| 88 | |
| 89 | |
| 90 | ### 谁在使用此项目 |
| 91 | |
| 92 | - Brook: https://github.com/txthinking/brook |
| 93 | - Shiliew: https://www.txthinking.com/shiliew.html |
| 94 | - dismap: https://github.com/zhzyker/dismap |
| 95 | - emp3r0r: https://github.com/jm33-m0/emp3r0r |
| 96 | - hysteria: https://github.com/apernet/hysteria |
| 97 | - mtg: https://github.com/9seconds/mtg |
| 98 | - trojan-go: https://github.com/p4gefau1t/trojan-go |
| 99 | |
| 100 | ## 开源协议 |
| 101 | |
| 102 | 基于 MIT 协议开源 |