|
|
@ -279,7 +279,7 @@ func writeSocks5Response(writer io.Writer, errCode byte, address net.Address, po
|
|
|
|
func writeSocks4Response(writer io.Writer, errCode byte, address net.Address, port net.Port) error {
|
|
|
|
func writeSocks4Response(writer io.Writer, errCode byte, address net.Address, port net.Port) error {
|
|
|
|
buffer := buf.NewLocal(32)
|
|
|
|
buffer := buf.NewLocal(32)
|
|
|
|
buffer.AppendBytes(0x00, errCode)
|
|
|
|
buffer.AppendBytes(0x00, errCode)
|
|
|
|
buffer.AppendSupplier(serial.WriteUint16(port.Value()))
|
|
|
|
common.Must(buffer.AppendSupplier(serial.WriteUint16(port.Value())))
|
|
|
|
buffer.Append(address.IP())
|
|
|
|
buffer.Append(address.IP())
|
|
|
|
_, err := writer.Write(buffer.Bytes())
|
|
|
|
_, err := writer.Write(buffer.Bytes())
|
|
|
|
return err
|
|
|
|
return err
|
|
|
@ -392,6 +392,40 @@ func (w *UDPWriter) Write(b []byte) (int, error) {
|
|
|
|
return len(b), nil
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func ReadAddress(b *buf.Buffer, addrType byte, reader io.Reader) (net.Address, net.Port, error) {
|
|
|
|
|
|
|
|
var address net.Address
|
|
|
|
|
|
|
|
switch addrType {
|
|
|
|
|
|
|
|
case addrTypeIPv4:
|
|
|
|
|
|
|
|
if err := b.AppendSupplier(buf.ReadFullFrom(reader, 4)); err != nil {
|
|
|
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
address = net.IPAddress(b.BytesFrom(-4))
|
|
|
|
|
|
|
|
case addrTypeIPv6:
|
|
|
|
|
|
|
|
if err := b.AppendSupplier(buf.ReadFullFrom(reader, 16)); err != nil {
|
|
|
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
address = net.IPAddress(b.BytesFrom(-16))
|
|
|
|
|
|
|
|
case addrTypeDomain:
|
|
|
|
|
|
|
|
if err := b.AppendSupplier(buf.ReadFullFrom(reader, 1)); err != nil {
|
|
|
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
domainLength := int(b.Byte(b.Len() - 1))
|
|
|
|
|
|
|
|
if err := b.AppendSupplier(buf.ReadFullFrom(reader, domainLength)); err != nil {
|
|
|
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
address = net.DomainAddress(string(b.BytesFrom(-domainLength)))
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return nil, 0, newError("unknown address type: ", addrType)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err := b.AppendSupplier(buf.ReadFullFrom(reader, 2)); err != nil {
|
|
|
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
port := net.PortFromBytes(b.BytesFrom(-2))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return address, port, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer io.Writer) (*protocol.RequestHeader, error) {
|
|
|
|
func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer io.Writer) (*protocol.RequestHeader, error) {
|
|
|
|
authByte := byte(authNotRequired)
|
|
|
|
authByte := byte(authNotRequired)
|
|
|
|
if request.User != nil {
|
|
|
|
if request.User != nil {
|
|
|
@ -444,7 +478,10 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i
|
|
|
|
command = byte(cmdUDPPort)
|
|
|
|
command = byte(cmdUDPPort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.AppendBytes(socks5Version, command, 0x00 /* reserved */)
|
|
|
|
b.AppendBytes(socks5Version, command, 0x00 /* reserved */)
|
|
|
|
AppendAddress(b, request.Address, request.Port)
|
|
|
|
if err := AppendAddress(b, request.Address, request.Port); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if _, err := writer.Write(b.Bytes()); err != nil {
|
|
|
|
if _, err := writer.Write(b.Bytes()); err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -463,35 +500,10 @@ func ClientHandshake(request *protocol.RequestHeader, reader io.Reader, writer i
|
|
|
|
|
|
|
|
|
|
|
|
b.Clear()
|
|
|
|
b.Clear()
|
|
|
|
|
|
|
|
|
|
|
|
var address net.Address
|
|
|
|
address, port, err := ReadAddress(b, addrType, reader)
|
|
|
|
switch addrType {
|
|
|
|
if err != nil {
|
|
|
|
case addrTypeIPv4:
|
|
|
|
|
|
|
|
if err := b.AppendSupplier(buf.ReadFullFrom(reader, 4)); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
address = net.IPAddress(b.Bytes())
|
|
|
|
|
|
|
|
case addrTypeIPv6:
|
|
|
|
|
|
|
|
if err := b.AppendSupplier(buf.ReadFullFrom(reader, 16)); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
address = net.IPAddress(b.Bytes())
|
|
|
|
|
|
|
|
case addrTypeDomain:
|
|
|
|
|
|
|
|
if err := b.AppendSupplier(buf.ReadFullFrom(reader, 1)); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
domainLength := int(b.Byte(0))
|
|
|
|
|
|
|
|
if err := b.AppendSupplier(buf.ReadFullFrom(reader, domainLength)); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
address = net.DomainAddress(string(b.BytesFrom(-domainLength)))
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return nil, newError("unknown address type: ", addrType)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err := b.AppendSupplier(buf.ReadFullFrom(reader, 2)); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
port := net.PortFromBytes(b.BytesFrom(-2))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if request.Command == protocol.RequestCommandUDP {
|
|
|
|
if request.Command == protocol.RequestCommandUDP {
|
|
|
|
udpRequest := &protocol.RequestHeader{
|
|
|
|
udpRequest := &protocol.RequestHeader{
|
|
|
|