2016-01-27 11:46:40 +00:00
|
|
|
// R.I.P Shadowsocks
|
|
|
|
|
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
2016-01-28 12:40:00 +00:00
|
|
|
"crypto/rand"
|
2016-01-28 22:58:23 +00:00
|
|
|
"io"
|
2016-01-28 11:33:58 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/app"
|
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2016-01-27 21:11:31 +00:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2016-01-27 11:46:40 +00:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2016-01-27 21:11:31 +00:00
|
|
|
"github.com/v2ray/v2ray-core/proxy"
|
2016-01-28 11:33:58 +00:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/internal"
|
2016-01-28 16:08:32 +00:00
|
|
|
"github.com/v2ray/v2ray-core/transport/hub"
|
2016-01-27 11:46:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Shadowsocks struct {
|
2016-01-28 20:30:05 +00:00
|
|
|
space app.Space
|
|
|
|
config *Config
|
|
|
|
port v2net.Port
|
|
|
|
accepting bool
|
|
|
|
tcpHub *hub.TCPHub
|
|
|
|
udpHub *hub.UDPHub
|
2016-01-27 11:46:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Shadowsocks) Port() v2net.Port {
|
|
|
|
return this.port
|
|
|
|
}
|
|
|
|
|
2016-01-27 21:11:31 +00:00
|
|
|
func (this *Shadowsocks) Close() {
|
|
|
|
this.accepting = false
|
2016-01-28 20:30:05 +00:00
|
|
|
this.tcpHub.Close()
|
|
|
|
this.tcpHub = nil
|
2016-01-28 20:30:46 +00:00
|
|
|
|
|
|
|
this.udpHub.Close()
|
|
|
|
this.udpHub = nil
|
2016-01-27 21:11:31 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 11:46:40 +00:00
|
|
|
func (this *Shadowsocks) Listen(port v2net.Port) error {
|
2016-01-27 21:11:31 +00:00
|
|
|
if this.accepting {
|
|
|
|
if this.port == port {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return proxy.ErrorAlreadyListening
|
|
|
|
}
|
|
|
|
}
|
2016-01-28 20:30:05 +00:00
|
|
|
this.accepting = true
|
2016-01-27 21:11:31 +00:00
|
|
|
|
2016-01-28 20:30:05 +00:00
|
|
|
tcpHub, err := hub.ListenTCP(port, this.handleConnection)
|
2016-01-27 21:11:31 +00:00
|
|
|
if err != nil {
|
2016-01-28 20:30:05 +00:00
|
|
|
log.Error("Shadowsocks: Failed to listen TCP on port ", port, ": ", err)
|
2016-01-27 21:11:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-01-28 20:30:05 +00:00
|
|
|
this.tcpHub = tcpHub
|
|
|
|
|
|
|
|
if this.config.UDP {
|
|
|
|
udpHub, err := hub.ListenUDP(port, this.handlerUDPPayload)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to listen UDP on port ", port, ": ", err)
|
|
|
|
}
|
|
|
|
this.udpHub = udpHub
|
|
|
|
}
|
|
|
|
|
2016-01-27 11:46:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-01-27 21:11:31 +00:00
|
|
|
|
2016-01-28 20:30:05 +00:00
|
|
|
func (this *Shadowsocks) handlerUDPPayload(payload *alloc.Buffer, dest v2net.Destination) {
|
|
|
|
defer payload.Release()
|
|
|
|
|
|
|
|
iv := payload.Value[:this.config.Cipher.IVSize()]
|
|
|
|
key := this.config.Key
|
|
|
|
payload.SliceFrom(this.config.Cipher.IVSize())
|
|
|
|
|
|
|
|
reader, err := this.config.Cipher.NewDecodingStream(key, iv, payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to create decoding stream: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
request, err := ReadRequest(reader)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer, _ := v2net.ReadFrom(reader, nil)
|
|
|
|
|
|
|
|
packet := v2net.NewPacket(v2net.TCPDestination(request.Address, request.Port), buffer, false)
|
|
|
|
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
|
|
|
|
close(ray.InboundInput())
|
|
|
|
|
|
|
|
for respChunk := range ray.InboundOutput() {
|
|
|
|
|
|
|
|
response := alloc.NewBuffer().Slice(0, this.config.Cipher.IVSize())
|
|
|
|
rand.Read(response.Value)
|
|
|
|
|
|
|
|
writer, err := this.config.Cipher.NewEncodingStream(key, response.Value, response)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to create encoding stream: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case request.Address.IsIPv4():
|
|
|
|
writer.Write([]byte{AddrTypeIPv4})
|
|
|
|
writer.Write(request.Address.IP())
|
|
|
|
case request.Address.IsIPv6():
|
|
|
|
writer.Write([]byte{AddrTypeIPv6})
|
|
|
|
writer.Write(request.Address.IP())
|
|
|
|
case request.Address.IsDomain():
|
|
|
|
writer.Write([]byte{AddrTypeDomain, byte(len(request.Address.Domain()))})
|
|
|
|
writer.Write([]byte(request.Address.Domain()))
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Write(request.Port.Bytes())
|
|
|
|
writer.Write(respChunk.Value)
|
|
|
|
respChunk.Release()
|
|
|
|
|
|
|
|
this.udpHub.WriteTo(response.Value, dest)
|
|
|
|
response.Release()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-28 16:08:32 +00:00
|
|
|
func (this *Shadowsocks) handleConnection(conn *hub.TCPConn) {
|
2016-01-27 21:11:31 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
2016-01-28 11:33:58 +00:00
|
|
|
buffer := alloc.NewSmallBuffer()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
2016-01-28 22:58:23 +00:00
|
|
|
_, err := io.ReadFull(conn, buffer.Value[:this.config.Cipher.IVSize()])
|
2016-01-28 11:33:58 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to read IV: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
iv := buffer.Value[:this.config.Cipher.IVSize()]
|
|
|
|
key := this.config.Key
|
|
|
|
|
|
|
|
reader, err := this.config.Cipher.NewDecodingStream(key, iv, conn)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to create decoding stream: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
request, err := ReadRequest(reader)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
packet := v2net.NewPacket(v2net.TCPDestination(request.Address, request.Port), nil, true)
|
|
|
|
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
|
|
|
|
|
|
|
|
var writeFinish sync.Mutex
|
|
|
|
writeFinish.Lock()
|
|
|
|
go func() {
|
2016-01-28 20:30:05 +00:00
|
|
|
firstChunk := alloc.NewBuffer().Slice(0, this.config.Cipher.IVSize())
|
2016-01-28 12:40:00 +00:00
|
|
|
defer firstChunk.Release()
|
|
|
|
|
2016-01-28 20:30:05 +00:00
|
|
|
writer, err := this.config.Cipher.NewEncodingStream(key, firstChunk.Value, conn)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to create encoding stream: ", err)
|
|
|
|
return
|
|
|
|
}
|
2016-01-28 12:40:00 +00:00
|
|
|
|
|
|
|
if payload, ok := <-ray.InboundOutput(); ok {
|
|
|
|
firstChunk.Append(payload.Value)
|
|
|
|
payload.Release()
|
|
|
|
|
|
|
|
writer.Write(firstChunk.Value)
|
|
|
|
v2net.ChanToWriter(writer, ray.InboundOutput())
|
|
|
|
}
|
2016-01-28 11:33:58 +00:00
|
|
|
writeFinish.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
v2net.ReaderToChan(ray.InboundInput(), reader)
|
|
|
|
close(ray.InboundInput())
|
|
|
|
|
|
|
|
writeFinish.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
internal.MustRegisterInboundHandlerCreator("shadowsocks",
|
|
|
|
func(space app.Space, rawConfig interface{}) (proxy.InboundHandler, error) {
|
|
|
|
config := rawConfig.(*Config)
|
|
|
|
return &Shadowsocks{
|
|
|
|
space: space,
|
|
|
|
config: config,
|
|
|
|
}, nil
|
|
|
|
})
|
2016-01-27 21:11:31 +00:00
|
|
|
}
|