Skip to content

Commit

Permalink
Client: resolve domain on server side
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Feb 15, 2023
1 parent e0c1ba4 commit 1d06f3a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 86 deletions.
16 changes: 3 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Client struct {
RemoteAddress net.Addr
TCPTimeout int
UDPTimeout int
Dst string
}

// This is just create a client, you need to use Dial to create conn
Expand All @@ -42,16 +43,11 @@ func (c *Client) DialWithLocalAddr(network, src, dst string, remoteAddr net.Addr
Password: c.Password,
TCPTimeout: c.TCPTimeout,
UDPTimeout: c.UDPTimeout,
Dst: dst,
RemoteAddress: remoteAddr,
}
var err error
if network == "tcp" {
if c.RemoteAddress == nil {
c.RemoteAddress, err = Resolve("tcp", dst)
if err != nil {
return nil, err
}
}
var laddr net.Addr
if src != "" {
laddr, err = net.ResolveTCPAddr("tcp", src)
Expand All @@ -75,12 +71,6 @@ func (c *Client) DialWithLocalAddr(network, src, dst string, remoteAddr net.Addr
return c, nil
}
if network == "udp" {
if c.RemoteAddress == nil {
c.RemoteAddress, err = Resolve("udp", dst)
if err != nil {
return nil, err
}
}
var laddr net.Addr
if src != "" {
laddr, err = net.ResolveTCPAddr("tcp", src)
Expand Down Expand Up @@ -138,7 +128,7 @@ func (c *Client) Write(b []byte) (int, error) {
if c.UDPConn == nil {
return c.TCPConn.Write(b)
}
a, h, p, err := ParseAddress(c.RemoteAddress.String())
a, h, p, err := ParseAddress(c.Dst)
if err != nil {
return 0, err
}
Expand Down
59 changes: 9 additions & 50 deletions client_side.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,14 @@ func NewNegotiationRequest(methods []byte) *NegotiationRequest {

// WriteTo write negotiation request packet into server
func (r *NegotiationRequest) WriteTo(w io.Writer) (int64, error) {
var n int
i, err := w.Write([]byte{r.Ver})
n = n + i
i, err := w.Write(append([]byte{r.Ver, r.NMethods}, r.Methods...))
if err != nil {
return int64(n), err
}
i, err = w.Write([]byte{r.NMethods})
n = n + i
if err != nil {
return int64(n), err
}
i, err = w.Write(r.Methods)
n = n + i
if err != nil {
return int64(n), err
return 0, err
}
if Debug {
log.Printf("Sent NegotiationRequest: %#v %#v %#v\n", r.Ver, r.NMethods, r.Methods)
}
return int64(n), nil
return int64(i), nil
}

// NewNegotiationReplyFrom read negotiation reply packet from server
Expand Down Expand Up @@ -75,31 +63,14 @@ func NewUserPassNegotiationRequest(username []byte, password []byte) *UserPassNe

// WriteTo write user password negotiation request packet into server
func (r *UserPassNegotiationRequest) WriteTo(w io.Writer) (int64, error) {
var n int
i, err := w.Write([]byte{r.Ver, r.Ulen})
n = n + i
if err != nil {
return int64(n), err
}
i, err = w.Write(r.Uname)
n = n + i
if err != nil {
return int64(n), err
}
i, err = w.Write([]byte{r.Plen})
n = n + i
if err != nil {
return int64(n), err
}
i, err = w.Write(r.Passwd)
n = n + i
i, err := w.Write(append(append(append([]byte{r.Ver, r.Ulen}, r.Uname...), r.Plen), r.Passwd...))
if err != nil {
return int64(n), err
return 0, err
}
if Debug {
log.Printf("Sent UserNameNegotiationRequest: %#v %#v %#v %#v %#v\n", r.Ver, r.Ulen, r.Uname, r.Plen, r.Passwd)
}
return int64(n), nil
return int64(i), nil
}

// NewUserPassNegotiationReplyFrom read user password negotiation reply packet from server
Expand Down Expand Up @@ -137,26 +108,14 @@ func NewRequest(cmd byte, atyp byte, dstaddr []byte, dstport []byte) *Request {

// WriteTo write request packet into server
func (r *Request) WriteTo(w io.Writer) (int64, error) {
var n int
i, err := w.Write([]byte{r.Ver, r.Cmd, r.Rsv, r.Atyp})
n = n + i
if err != nil {
return int64(n), err
}
i, err = w.Write(r.DstAddr)
n = n + i
if err != nil {
return int64(n), err
}
i, err = w.Write(r.DstPort)
n = n + i
i, err := w.Write(append(append([]byte{r.Ver, r.Cmd, r.Rsv, r.Atyp}, r.DstAddr...), r.DstPort...))
if err != nil {
return int64(n), err
return 0, err
}
if Debug {
log.Printf("Sent Request: %#v %#v %#v %#v %#v %#v\n", r.Ver, r.Cmd, r.Rsv, r.Atyp, r.DstAddr, r.DstPort)
}
return int64(n), nil
return int64(i), nil
}

// NewReplyFrom read reply packet from server
Expand Down
30 changes: 7 additions & 23 deletions server_side.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@ func NewNegotiationReply(method byte) *NegotiationReply {

// WriteTo write negotiation reply packet into client
func (r *NegotiationReply) WriteTo(w io.Writer) (int64, error) {
var n int
i, err := w.Write([]byte{r.Ver, r.Method})
n = n + i
if err != nil {
return int64(n), err
return 0, err
}
if Debug {
log.Printf("Sent NegotiationReply: %#v %#v\n", r.Ver, r.Method)
}
return int64(n), nil
return int64(i), nil
}

// NewUserPassNegotiationRequestFrom read user password negotiation request packet from client
Expand Down Expand Up @@ -109,16 +107,14 @@ func NewUserPassNegotiationReply(status byte) *UserPassNegotiationReply {

// WriteTo write negotiation username password reply packet into client
func (r *UserPassNegotiationReply) WriteTo(w io.Writer) (int64, error) {
var n int
i, err := w.Write([]byte{r.Ver, r.Status})
n = n + i
if err != nil {
return int64(n), err
return 0, err
}
if Debug {
log.Printf("Sent UserPassNegotiationReply: %#v %#v \n", r.Ver, r.Status)
}
return int64(n), nil
return int64(i), nil
}

// NewRequestFrom read requst packet from client
Expand Down Expand Up @@ -191,26 +187,14 @@ func NewReply(rep byte, atyp byte, bndaddr []byte, bndport []byte) *Reply {

// WriteTo write reply packet into client
func (r *Reply) WriteTo(w io.Writer) (int64, error) {
var n int
i, err := w.Write([]byte{r.Ver, r.Rep, r.Rsv, r.Atyp})
n = n + i
i, err := w.Write(append(append([]byte{r.Ver, r.Rep, r.Rsv, r.Atyp}, r.BndAddr...), r.BndPort...))
if err != nil {
return int64(n), err
}
i, err = w.Write(r.BndAddr)
n = n + i
if err != nil {
return int64(n), err
}
i, err = w.Write(r.BndPort)
n = n + i
if err != nil {
return int64(n), err
return 0, err
}
if Debug {
log.Printf("Sent Reply: %#v %#v %#v %#v %#v %#v\n", r.Ver, r.Rep, r.Rsv, r.Atyp, r.BndAddr, r.BndPort)
}
return int64(n), nil
return int64(i), nil
}

func NewDatagramFromBytes(bb []byte) (*Datagram, error) {
Expand Down

0 comments on commit 1d06f3a

Please sign in to comment.