2016-01-27 06:46:40 -05:00
|
|
|
// R.I.P Shadowsocks
|
|
|
|
|
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
2016-01-28 07:40:00 -05:00
|
|
|
"crypto/rand"
|
2016-01-28 06:33:58 -05:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/app"
|
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2016-01-27 16:11:31 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2016-01-27 06:46:40 -05:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2016-01-27 16:11:31 -05:00
|
|
|
"github.com/v2ray/v2ray-core/proxy"
|
2016-01-28 06:33:58 -05:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/internal"
|
2016-01-27 16:11:31 -05:00
|
|
|
"github.com/v2ray/v2ray-core/transport/listener"
|
2016-01-27 06:46:40 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Shadowsocks struct {
|
2016-01-28 06:33:58 -05:00
|
|
|
space app.Space
|
2016-01-27 16:11:31 -05:00
|
|
|
config *Config
|
|
|
|
port v2net.Port
|
|
|
|
accepting bool
|
|
|
|
tcpListener *listener.TCPListener
|
2016-01-27 06:46:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Shadowsocks) Port() v2net.Port {
|
|
|
|
return this.port
|
|
|
|
}
|
|
|
|
|
2016-01-27 16:11:31 -05:00
|
|
|
func (this *Shadowsocks) Close() {
|
|
|
|
this.accepting = false
|
|
|
|
this.tcpListener.Close()
|
|
|
|
this.tcpListener = nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 06:46:40 -05:00
|
|
|
func (this *Shadowsocks) Listen(port v2net.Port) error {
|
2016-01-27 16:11:31 -05:00
|
|
|
if this.accepting {
|
|
|
|
if this.port == port {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return proxy.ErrorAlreadyListening
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tcpListener, err := listener.ListenTCP(port, this.handleConnection)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to listen on port ", port, ": ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
this.tcpListener = tcpListener
|
|
|
|
this.accepting = true
|
2016-01-27 06:46:40 -05:00
|
|
|
return nil
|
|
|
|
}
|
2016-01-27 16:11:31 -05:00
|
|
|
|
|
|
|
func (this *Shadowsocks) handleConnection(conn *listener.TCPConn) {
|
|
|
|
defer conn.Close()
|
|
|
|
|
2016-01-28 06:33:58 -05:00
|
|
|
buffer := alloc.NewSmallBuffer()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
|
|
|
_, err := v2net.ReadAllBytes(conn, buffer.Value[:this.config.Cipher.IVSize()])
|
|
|
|
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)
|
|
|
|
|
2016-01-28 07:40:00 -05:00
|
|
|
respIv := make([]byte, this.config.Cipher.IVSize())
|
|
|
|
rand.Read(respIv)
|
|
|
|
|
|
|
|
writer, err := this.config.Cipher.NewEncodingStream(key, respIv, conn)
|
2016-01-28 06:33:58 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to create encoding stream: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var writeFinish sync.Mutex
|
|
|
|
writeFinish.Lock()
|
|
|
|
go func() {
|
2016-01-28 07:40:00 -05:00
|
|
|
firstChunk := alloc.NewBuffer().Clear()
|
|
|
|
defer firstChunk.Release()
|
|
|
|
|
|
|
|
firstChunk.Append(respIv)
|
|
|
|
|
|
|
|
if payload, ok := <-ray.InboundOutput(); ok {
|
|
|
|
firstChunk.Append(payload.Value)
|
|
|
|
payload.Release()
|
|
|
|
|
|
|
|
writer.Write(firstChunk.Value)
|
|
|
|
v2net.ChanToWriter(writer, ray.InboundOutput())
|
|
|
|
}
|
2016-01-28 06:33:58 -05: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 16:11:31 -05:00
|
|
|
}
|