mirror of
https://github.com/p4gefau1t/trojan-go.git
synced 2024-04-21 12:21:34 +00:00
fix udp parsing
This commit is contained in:
+9
-9
@@ -83,7 +83,7 @@ func NewAddress(host string, port int, network string) *Address {
|
||||
|
||||
func (a *Address) Marshal(r io.Reader) error {
|
||||
byteBuf := [1]byte{}
|
||||
_, err := r.Read(byteBuf[:])
|
||||
_, err := io.ReadFull(r, byteBuf[:])
|
||||
if err != nil {
|
||||
return NewError("Unable to read ATYPE").Base(err)
|
||||
}
|
||||
@@ -91,30 +91,30 @@ func (a *Address) Marshal(r io.Reader) error {
|
||||
switch a.AddressType {
|
||||
case IPv4:
|
||||
var buf [6]byte
|
||||
_, err := r.Read(buf[:])
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return NewError("Failed to read ipv4").Base(err)
|
||||
return NewError("Failed to read IPv4").Base(err)
|
||||
}
|
||||
a.IP = buf[0:4]
|
||||
a.Port = int(binary.BigEndian.Uint16(buf[4:6]))
|
||||
case IPv6:
|
||||
var buf [18]byte
|
||||
_, err := r.Read(buf[:])
|
||||
_, err := io.ReadFull(r, buf[:])
|
||||
if err != nil {
|
||||
return NewError("Failed to read ipv6").Base(err)
|
||||
return NewError("Failed to read IPv6").Base(err)
|
||||
}
|
||||
a.IP = buf[0:16]
|
||||
a.Port = int(binary.BigEndian.Uint16(buf[16:18]))
|
||||
case DomainName:
|
||||
_, err := r.Read(byteBuf[:])
|
||||
_, err := io.ReadFull(r, byteBuf[:])
|
||||
length := byteBuf[0]
|
||||
if err != nil {
|
||||
return NewError("Failed to read length")
|
||||
return NewError("Failed to read domain name length")
|
||||
}
|
||||
buf := make([]byte, length+2)
|
||||
_, err = r.Read(buf)
|
||||
_, err = io.ReadFull(r, buf)
|
||||
if err != nil {
|
||||
return NewError("Failed to read domain")
|
||||
return NewError("Failed to read domain name")
|
||||
}
|
||||
//the fucking browser uses IP as a domain name sometimes
|
||||
host := buf[0:length]
|
||||
|
||||
@@ -35,7 +35,7 @@ type Request struct {
|
||||
|
||||
func (r *Request) Marshal(rr io.Reader) error {
|
||||
byteBuf := [1]byte{}
|
||||
_, err := rr.Read(byteBuf[:])
|
||||
_, err := io.ReadFull(rr, byteBuf[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -70,7 +70,6 @@ func (i *TrojanInboundConnSession) parseRequest(r *common.RewindReader) error {
|
||||
|
||||
crlf := [2]byte{}
|
||||
_, err = io.ReadFull(r, crlf[:])
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ func (o *TrojanOutboundConnSession) writeRequest() {
|
||||
hash := user.Hash()
|
||||
o.meter = user
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 128))
|
||||
crlf := []byte("\r\n")
|
||||
crlf := []byte{0x0d, 0x0a}
|
||||
buf.Write([]byte(hash))
|
||||
buf.Write(crlf)
|
||||
o.request.Unmarshal(buf)
|
||||
|
||||
@@ -29,6 +29,10 @@ func (i *TrojanPacketSession) ReadPacket() (*protocol.Request, []byte, error) {
|
||||
return req, nil, common.NewError("Failed to read length")
|
||||
}
|
||||
length := binary.BigEndian.Uint16(lengthBuf[:])
|
||||
|
||||
crlf := [2]byte{}
|
||||
io.ReadFull(i.conn, crlf[:])
|
||||
|
||||
packet := make([]byte, length)
|
||||
_, err = io.ReadFull(i.conn, packet)
|
||||
if err != nil {
|
||||
@@ -38,13 +42,19 @@ func (i *TrojanPacketSession) ReadPacket() (*protocol.Request, []byte, error) {
|
||||
}
|
||||
|
||||
func (i *TrojanPacketSession) WritePacket(req *protocol.Request, packet []byte) (int, error) {
|
||||
buf := bytes.NewBuffer(make([]byte, 0, 512))
|
||||
buf := bytes.NewBuffer(make([]byte, 0, len(packet)+32))
|
||||
common.Must(req.Address.Unmarshal(buf))
|
||||
|
||||
length := len(packet)
|
||||
lengthBuf := [2]byte{}
|
||||
binary.BigEndian.PutUint16(lengthBuf[:], uint16(length))
|
||||
buf.Write(lengthBuf[:])
|
||||
|
||||
crlf := [2]byte{0x0d, 0x0a}
|
||||
buf.Write(crlf[:])
|
||||
|
||||
buf.Write(packet)
|
||||
|
||||
return i.conn.Write(buf.Bytes())
|
||||
}
|
||||
|
||||
|
||||
+1
-38
@@ -21,7 +21,6 @@ import (
|
||||
"github.com/p4gefau1t/trojan-go/common"
|
||||
"github.com/p4gefau1t/trojan-go/conf"
|
||||
_ "github.com/p4gefau1t/trojan-go/log/golog"
|
||||
tp "github.com/p4gefau1t/trojan-go/proxy"
|
||||
"github.com/p4gefau1t/trojan-go/proxy/client"
|
||||
"github.com/p4gefau1t/trojan-go/proxy/server"
|
||||
|
||||
@@ -141,7 +140,7 @@ func getBasicServerConfig() *conf.GlobalConfig {
|
||||
|
||||
func getBasicClientConfig() *conf.GlobalConfig {
|
||||
config := &conf.GlobalConfig{
|
||||
LocalAddress: common.NewAddress("127.0.0.1", 4444, "tcp"),
|
||||
LocalAddress: common.NewAddress("0.0.0.0", 4444, "tcp"),
|
||||
RemoteAddress: common.NewAddress("127.0.0.1", 4445, "tcp"),
|
||||
TLS: getTLSConfig(),
|
||||
Hash: getHash("trojanpassword"),
|
||||
@@ -380,28 +379,6 @@ func MultiThreadSpeedTestClientServer(b *testing.B, clientConfig *conf.GlobalCon
|
||||
cancel()
|
||||
}
|
||||
|
||||
func TestRealProxy(t *testing.T) {
|
||||
if os.Getenv("real_test") == "" {
|
||||
t.Skip("skipping real proxy test")
|
||||
}
|
||||
clientConfig := addMuxConfig(getBasicClientConfig())
|
||||
serverConfig := getBasicServerConfig()
|
||||
go RunClient(context.Background(), clientConfig)
|
||||
go RunHelloHTTPServer(context.Background())
|
||||
RunServer(context.Background(), serverConfig)
|
||||
}
|
||||
|
||||
func TestRealClient(t *testing.T) {
|
||||
if os.Getenv("real_test") == "" {
|
||||
t.Skip("skipping real proxy test")
|
||||
}
|
||||
b, err := ioutil.ReadFile("/etc/trojan-go/config.json")
|
||||
common.Must(err)
|
||||
config, err := conf.ParseJSON(b)
|
||||
common.Must(err)
|
||||
RunClient(context.Background(), config)
|
||||
}
|
||||
|
||||
func TestNormal(t *testing.T) {
|
||||
clientConfig := getBasicClientConfig()
|
||||
serverConfig := getBasicServerConfig()
|
||||
@@ -575,17 +552,3 @@ func TestDNS(t *testing.T) {
|
||||
conn.Close()
|
||||
cancel()
|
||||
}
|
||||
|
||||
func TestJSON(t *testing.T) {
|
||||
if os.Getenv("CONFIG") == "" {
|
||||
t.Skip("skip json test")
|
||||
}
|
||||
configFile1 := "/etc/trojan-go/config.json"
|
||||
configBytes1, err := ioutil.ReadFile(configFile1)
|
||||
common.Must(err)
|
||||
config1, err := conf.ParseJSON(configBytes1)
|
||||
common.Must(err)
|
||||
r, err := tp.NewProxy(config1)
|
||||
common.Must(err)
|
||||
r.Run()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/p4gefau1t/trojan-go/common"
|
||||
"github.com/p4gefau1t/trojan-go/conf"
|
||||
)
|
||||
|
||||
func TestRealProxy(t *testing.T) {
|
||||
if os.Getenv("real_test") == "" {
|
||||
t.Skip("skipping real proxy test")
|
||||
}
|
||||
clientConfig := addMuxConfig(getBasicClientConfig())
|
||||
serverConfig := getBasicServerConfig()
|
||||
go RunClient(context.Background(), clientConfig)
|
||||
go RunHelloHTTPServer(context.Background())
|
||||
RunServer(context.Background(), serverConfig)
|
||||
}
|
||||
|
||||
func TestRealClient(t *testing.T) {
|
||||
if os.Getenv("real_test") == "" {
|
||||
t.Skip("skipping real proxy test")
|
||||
}
|
||||
b, err := ioutil.ReadFile("client.json")
|
||||
common.Must(err)
|
||||
config, err := conf.ParseJSON(b)
|
||||
common.Must(err)
|
||||
RunClient(context.Background(), config)
|
||||
}
|
||||
|
||||
func TestRealServer(t *testing.T) {
|
||||
if os.Getenv("real_test") == "" {
|
||||
t.Skip("skipping real proxy test")
|
||||
}
|
||||
b, err := ioutil.ReadFile("server.json")
|
||||
common.Must(err)
|
||||
config, err := conf.ParseJSON(b)
|
||||
common.Must(err)
|
||||
RunServer(context.Background(), config)
|
||||
}
|
||||
Reference in New Issue
Block a user