forked from txthinking/socks5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c9252b9
commit bff9f50
Showing
12 changed files
with
317 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,83 @@ | ||
# socks5 | ||
SOCKS Protocol Version 5 | ||
## socks5 | ||
|
||
[](https://goreportcard.com/report/github.com/txthinking/socks5) | ||
[](https://godoc.org/github.com/txthinking/socks5) | ||
|
||
SOCKS Protocol Version 5 Library | ||
|
||
### Install | ||
``` | ||
$ go get github.com/txthinking/socks5 | ||
``` | ||
|
||
### Example | ||
|
||
``` | ||
func ExampleSocks5Server() { | ||
timeout := 60 // 60s | ||
socks5.Debug = true // enable socks5 debug log | ||
l, err := net.Listen("tcp", ":1980") | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
defer l.Close() | ||
for { | ||
c, err := l.Accept() | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
go func(c net.Conn) { | ||
defer c.Close() | ||
if err := c.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second)); err != nil { | ||
log.Println("set local timeout:", err) | ||
return | ||
} | ||
s5s := &socks5.Server{ | ||
C: c, | ||
SelectMethod: func(methods []byte) (method byte, got bool) { | ||
for _, m := range methods { | ||
if m == socks5.MethodNone { | ||
method = socks5.MethodNone | ||
got = true | ||
return | ||
} | ||
} | ||
return | ||
}, | ||
SupportedCommands: []byte{socks5.CmdConnect}, | ||
} | ||
if err := s5s.Negotiate(); err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
r, err := s5s.GetRequest() | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
rc, err := r.Connect(c) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
defer rc.Close() | ||
if err := rc.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second)); err != nil { | ||
log.Println("set remote timeout:", err) | ||
return | ||
} | ||
go func() { | ||
_, _ = io.Copy(c, rc) | ||
}() | ||
_, _ = io.Copy(rc, c) | ||
}(c) | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package socks5 | ||
|
||
import "net" | ||
import ( | ||
"errors" | ||
"net" | ||
) | ||
|
||
// todo | ||
// TODO | ||
func (r *Request) bind(c net.Conn) error { | ||
return nil | ||
return errors.New("Unsupport BIND now") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package socks5_test | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"net" | ||
"time" | ||
|
||
"github.com/txthinking/socks5" | ||
) | ||
|
||
func ExampleSocks5Server() { | ||
timeout := 60 // 60s | ||
socks5.Debug = true // enable socks5 debug log | ||
|
||
l, err := net.Listen("tcp", ":1980") | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
defer l.Close() | ||
|
||
for { | ||
c, err := l.Accept() | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
go func(c net.Conn) { | ||
defer c.Close() | ||
if err := c.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second)); err != nil { | ||
log.Println("set local timeout:", err) | ||
return | ||
} | ||
|
||
s5s := &socks5.Server{ | ||
C: c, | ||
SelectMethod: func(methods []byte) (method byte, got bool) { | ||
for _, m := range methods { | ||
if m == socks5.MethodNone { | ||
method = socks5.MethodNone | ||
got = true | ||
return | ||
} | ||
} | ||
return | ||
}, | ||
SupportedCommands: []byte{socks5.CmdConnect}, | ||
} | ||
if err := s5s.Negotiate(); err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
r, err := s5s.GetRequest() | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
rc, err := r.Connect(c) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
defer rc.Close() | ||
if err := rc.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second)); err != nil { | ||
log.Println("set remote timeout:", err) | ||
return | ||
} | ||
go func() { | ||
_, _ = io.Copy(c, rc) | ||
}() | ||
_, _ = io.Copy(rc, c) | ||
|
||
}(c) | ||
|
||
} | ||
} |
Oops, something went wrong.