-
Notifications
You must be signed in to change notification settings - Fork 12
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
Showing
13 changed files
with
756 additions
and
18 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
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ package shadowsocks_test | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
|
@@ -12,6 +13,7 @@ import ( | |
) | ||
|
||
var list = []string{ | ||
"dummy", | ||
"aes-128-cfb", | ||
"aes-128-ctr", | ||
"aes-128-gcm", | ||
|
@@ -78,3 +80,87 @@ func TestAll(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestEncryptor(t *testing.T) { | ||
var tmp1 [255]byte | ||
var tmp2 [255]byte | ||
for _, c := range list { | ||
t.Run(c, func(t *testing.T) { | ||
cipher, err := shadowsocks.NewCipher(c, "pwd") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
n1, err := cipher.Encrypt(tmp1[:], []byte(c)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
n2, err := cipher.Decrypt(tmp2[:], tmp1[:n1]) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(tmp2[:n2]) != c { | ||
t.Errorf("%q %q %q", c, tmp1[:n1], tmp2[:n2]) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestPacket(t *testing.T) { | ||
// echo server | ||
p, err := net.ListenPacket("udp", "127.0.0.1:0") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
go func() { | ||
var buf [1024 * 32]byte | ||
for { | ||
i, addr, err := p.ReadFrom(buf[:]) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
tmp := append([]byte("echo "), buf[:i]...) | ||
_, err = p.WriteTo(tmp, addr) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
}() | ||
|
||
remote, err := shadowsocks.NewSimplePacketServer("ss://aes-128-cfb:[email protected]:0") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = remote.Start(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
t.Log(remote.ProxyURL()) | ||
local, err := shadowsocks.NewPacketClient(remote.ProxyURL()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
client, err := local.ListenPacket(context.Background(), "udp", ":0") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
for i := 0; i != 10; i++ { | ||
tmp := fmt.Sprintf("hello %d", i) | ||
_, err = client.WriteTo([]byte(tmp), p.LocalAddr()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
var buf [1024 * 32]byte | ||
i, addr, err := client.ReadFrom(buf[:]) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if "echo "+tmp != string(buf[:i]) { | ||
t.Error("resp", i, string(buf[:i]), addr) | ||
} | ||
} | ||
} |
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
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,27 @@ | ||
package dummy | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/wzshiming/shadowsocks" | ||
) | ||
|
||
func init() { | ||
shadowsocks.RegisterCipher("dummy", func(password string) (shadowsocks.ConnCipher, error) { | ||
return &cipher{}, nil | ||
}) | ||
} | ||
|
||
type cipher struct { | ||
} | ||
|
||
func (cipher) StreamConn(conn net.Conn) net.Conn { | ||
return conn | ||
} | ||
|
||
func (cipher) Decrypt(dist, src []byte) (n int, err error) { | ||
return copy(dist, src), nil | ||
} | ||
func (cipher) Encrypt(dist, src []byte) (n int, err error) { | ||
return copy(dist, src), nil | ||
} |
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,63 @@ | ||
package shadowsocks | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"net" | ||
) | ||
|
||
type ListenPacket interface { | ||
ListenPacket(ctx context.Context, network, address string) (net.PacketConn, error) | ||
} | ||
|
||
func decryptPacket(c ConnCipher, p BytesPool, dist, src []byte) (n int, addr net.Addr, err error) { | ||
i, err := c.Decrypt(dist, src) | ||
if err != nil { | ||
return 0, nil, err | ||
} | ||
buf := bytes.NewBuffer(dist[:i]) | ||
a, err := readAddress(buf) | ||
if err != nil { | ||
return 0, nil, err | ||
} | ||
i = copy(dist, buf.Bytes()) | ||
return i, a, nil | ||
} | ||
|
||
func encryptPacket(c ConnCipher, p BytesPool, dist, src []byte, addr net.Addr) (n int, err error) { | ||
a, err := parseAddress(addr.String()) | ||
if err != nil { | ||
return 0, err | ||
} | ||
buf := getBytes(p) | ||
defer putBytes(p, buf) | ||
b := bytes.NewBuffer(buf[:0]) | ||
err = writeAddress(b, a) | ||
if err != nil { | ||
return 0, err | ||
} | ||
b.Write(src) | ||
i, err := c.Encrypt(dist, b.Bytes()) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return i, nil | ||
} | ||
|
||
func toUDPAddr(addr net.Addr) (net.Addr, error) { | ||
switch a := addr.(type) { | ||
case *net.UDPAddr: | ||
return addr, nil | ||
case *address: | ||
return &net.UDPAddr{ | ||
IP: a.IP, | ||
Port: a.Port, | ||
}, nil | ||
default: | ||
a, err := net.ResolveUDPAddr("udp", addr.String()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return a, nil | ||
} | ||
} |
Oops, something went wrong.