Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Goals: KISS, less is more, small API, code is like the original protocol.
### Install

```
$ go get github.com/txthinking/socks5
$ go get github.com/txthinking/txthinkingsocks5
```

### Struct is like concept in protocol
Expand Down
2 changes: 1 addition & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ SOCKS Protocol Version 5 Library.

### 获取
```
$ go get github.com/txthinking/socks5
$ go get github.com/txthinking/txthinkingsocks5
```

### Struct的概念 对标 原始协议里的概念
Expand Down
2 changes: 1 addition & 1 deletion bind.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package socks5
package txthinkingsocks5

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package socks5
package txthinkingsocks5

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion client_side.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package socks5
package txthinkingsocks5

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion connect.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package socks5
package txthinkingsocks5

import (
"io"
Expand Down
18 changes: 9 additions & 9 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package socks5_test
package txthinkingsocks5_test

import (
"encoding/hex"
Expand All @@ -7,18 +7,18 @@ import (
"net"
"net/http"

socks5 "github.com/introspection3/txthinkingsocks5"
"github.com/miekg/dns"
"github.com/txthinking/socks5"
)

func ExampleServer() {
s, err := socks5.NewClassicServer("127.0.0.1:1080", "127.0.0.1", "", "", 0, 60)
if err != nil {
log.Println(err)
return
}
// You can pass in custom Handler
s.ListenAndServe(nil)
// s, err := socks5.NewClassicServer("127.0.0.1:1080", "127.0.0.1", "", "", 0, 60)
// if err != nil {
// log.Println(err)
// return
// }
// // You can pass in custom Handler
// s.ListenAndServe(nil)
// #Output:
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/txthinking/socks5
module github.com/introspection3/txthinkingsocks5

go 1.16
go 1.25

require (
github.com/miekg/dns v1.1.51
Expand Down
2 changes: 1 addition & 1 deletion init.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package socks5
package txthinkingsocks5

import (
"net"
Expand Down
109 changes: 73 additions & 36 deletions server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package socks5
package txthinkingsocks5

import (
"errors"
Expand All @@ -23,20 +23,20 @@ var (

// Server is socks5 server wrapper
type Server struct {
UserName string
Password string
Method byte
SupportedCommands []byte
Addr string
ServerAddr net.Addr
UDPConn *net.UDPConn
UDPExchanges *cache.Cache
TCPTimeout int
UDPTimeout int
Handle Handler
AssociatedUDP *cache.Cache
UDPSrc *cache.Cache
RunnerGroup *runnergroup.RunnerGroup
MethodUsernamePasswordEnabled bool
Accounts map[string]string
Method byte
SupportedCommands []byte
Addr string
ServerAddr net.Addr
UDPConn *net.UDPConn
UDPExchanges *cache.Cache
TCPTimeout int
UDPTimeout int
Handle Handler
AssociatedUDP *cache.Cache
UDPSrc *cache.Cache
RunnerGroup *runnergroup.RunnerGroup
// RFC: [UDP ASSOCIATE] The server MAY use this information to limit access to the association. Default false, no limit.
LimitUDP bool
}
Expand All @@ -48,7 +48,7 @@ type UDPExchange struct {
}

// NewClassicServer return a server which allow none method
func NewClassicServer(addr, ip, username, password string, tcpTimeout, udpTimeout int) (*Server, error) {
func NewClassicServer(addr, ip string, accounts map[string]string, tcpTimeout, udpTimeout int) (*Server, error) {
_, p, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
Expand All @@ -58,25 +58,29 @@ func NewClassicServer(addr, ip, username, password string, tcpTimeout, udpTimeou
return nil, err
}
m := MethodNone
if username != "" && password != "" {
var methodUsernamePasswordEnabled = false
if len(accounts) > 0 {
m = MethodUsernamePassword
methodUsernamePasswordEnabled = true

}
cs := cache.New(cache.NoExpiration, cache.NoExpiration)
cs1 := cache.New(cache.NoExpiration, cache.NoExpiration)
cs2 := cache.New(cache.NoExpiration, cache.NoExpiration)

s := &Server{
Method: m,
UserName: username,
Password: password,
SupportedCommands: []byte{CmdConnect, CmdUDP},
Addr: addr,
ServerAddr: saddr,
UDPExchanges: cs,
TCPTimeout: tcpTimeout,
UDPTimeout: udpTimeout,
AssociatedUDP: cs1,
UDPSrc: cs2,
RunnerGroup: runnergroup.New(),
Method: m,
Accounts: accounts,
MethodUsernamePasswordEnabled: methodUsernamePasswordEnabled,
SupportedCommands: []byte{CmdConnect, CmdUDP},
Addr: addr,
ServerAddr: saddr,
UDPExchanges: cs,
TCPTimeout: tcpTimeout,
UDPTimeout: udpTimeout,
AssociatedUDP: cs1,
UDPSrc: cs2,
RunnerGroup: runnergroup.New(),
}
return s, nil
}
Expand Down Expand Up @@ -107,12 +111,13 @@ func (s *Server) Negotiate(rw io.ReadWriter) error {
return err
}

if s.Method == MethodUsernamePassword {
if s.MethodUsernamePasswordEnabled {
urq, err := NewUserPassNegotiationRequestFrom(rw)
if err != nil {
return err
}
if string(urq.Uname) != s.UserName || string(urq.Passwd) != s.Password {
v, exist := s.Accounts[string(urq.Uname)]
if !exist || string(urq.Passwd) != v {
urp := NewUserPassNegotiationReply(UserPassStatusFailure)
if _, err := urp.WriteTo(rw); err != nil {
return err
Expand Down Expand Up @@ -268,7 +273,7 @@ func (h *DefaultHandle) TCPHandle(s *Server, c *net.TCPConn, r *Request) error {
}
defer rc.Close()
go func() {
var bf [1024 * 2]byte
var bf [1024 * 16]byte
for {
if s.TCPTimeout != 0 {
if err := rc.SetDeadline(time.Now().Add(time.Duration(s.TCPTimeout) * time.Second)); err != nil {
Expand All @@ -279,12 +284,28 @@ func (h *DefaultHandle) TCPHandle(s *Server, c *net.TCPConn, r *Request) error {
if err != nil {
return
}
if _, err := c.Write(bf[0:i]); err != nil {
// if _, err := c.Write(bf[0:i]); err != nil {
// return
// }
//--------------------------
n, err := c.Write(bf[0:i])
if err != nil {
return
}
if n < i {
// 处理未完全写入的情况
for n < i {
m, err := c.Write(bf[n:i])
if err != nil {
return
}
n += m
}
}
//--------------------------
}
}()
var bf [1024 * 2]byte
var bf [1024 * 16]byte
for {
if s.TCPTimeout != 0 {
if err := c.SetDeadline(time.Now().Add(time.Duration(s.TCPTimeout) * time.Second)); err != nil {
Expand All @@ -295,9 +316,25 @@ func (h *DefaultHandle) TCPHandle(s *Server, c *net.TCPConn, r *Request) error {
if err != nil {
return nil
}
if _, err := rc.Write(bf[0:i]); err != nil {
return nil
// if _, err := rc.Write(bf[0:i]); err != nil {
// return nil
// }
//--------------------------
n, err := rc.Write(bf[0:i])
if err != nil {
return err
}
if n < i {
// 处理未完全写入的情况
for n < i {
m, err := rc.Write(bf[n:i])
if err != nil {
return err
}
n += m
}
}
//--------------------------
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion server_side.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package socks5
package txthinkingsocks5

import (
"errors"
Expand Down
2 changes: 1 addition & 1 deletion socks5.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package socks5
package txthinkingsocks5

const (
// Ver is socks protocol version
Expand Down
2 changes: 1 addition & 1 deletion udp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package socks5
package txthinkingsocks5

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package socks5
package txthinkingsocks5

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion util_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package socks5
package txthinkingsocks5

import "testing"

Expand Down