1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-26 09:25:23 +00:00
v2fly/proxy/shadowsocks/shadowsocks.go

263 lines
7.1 KiB
Go
Raw Normal View History

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"
"io"
2016-01-28 11:33:58 +00:00
"sync"
"github.com/v2ray/v2ray-core/app"
2016-01-31 16:01:28 +00:00
"github.com/v2ray/v2ray-core/app/dispatcher"
2016-01-28 11:33:58 +00:00
"github.com/v2ray/v2ray-core/common/alloc"
2016-02-23 17:16:13 +00:00
"github.com/v2ray/v2ray-core/common/crypto"
2016-01-29 13:39:55 +00:00
v2io "github.com/v2ray/v2ray-core/common/io"
"github.com/v2ray/v2ray-core/common/log"
2016-01-27 11:46:40 +00:00
v2net "github.com/v2ray/v2ray-core/common/net"
2016-02-03 11:18:28 +00:00
"github.com/v2ray/v2ray-core/common/protocol"
2016-01-30 21:57:20 +00:00
"github.com/v2ray/v2ray-core/common/serial"
"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-31 16:01:28 +00:00
packetDispatcher dispatcher.PacketDispatcher
config *Config
port v2net.Port
accepting bool
tcpHub *hub.TCPHub
udpHub *hub.UDPHub
2016-02-01 20:34:07 +00:00
udpServer *hub.UDPServer
2016-01-31 16:01:28 +00:00
}
func NewShadowsocks(config *Config, packetDispatcher dispatcher.PacketDispatcher) *Shadowsocks {
return &Shadowsocks{
config: config,
packetDispatcher: packetDispatcher,
}
2016-01-27 11:46:40 +00:00
}
func (this *Shadowsocks) Port() v2net.Port {
return this.port
}
func (this *Shadowsocks) Close() {
this.accepting = false
2016-01-30 21:50:36 +00:00
// TODO: synchronization
if this.tcpHub != nil {
this.tcpHub.Close()
this.tcpHub = nil
}
if this.udpHub != nil {
this.udpHub.Close()
this.udpHub = nil
}
2016-01-28 20:30:46 +00:00
}
2016-01-27 11:46:40 +00:00
func (this *Shadowsocks) Listen(port v2net.Port) error {
if this.accepting {
if this.port == port {
return nil
} else {
return proxy.ErrorAlreadyListening
}
}
tcpHub, err := hub.ListenTCP(port, this.handleConnection, nil)
if err != nil {
2016-01-28 20:30:05 +00:00
log.Error("Shadowsocks: Failed to listen TCP on port ", port, ": ", err)
return err
}
2016-01-28 20:30:05 +00:00
this.tcpHub = tcpHub
if this.config.UDP {
2016-02-03 21:42:58 +00:00
this.udpServer = hub.NewUDPServer(this.packetDispatcher)
2016-01-28 20:30:05 +00:00
udpHub, err := hub.ListenUDP(port, this.handlerUDPPayload)
if err != nil {
log.Error("Shadowsocks: Failed to listen UDP on port ", port, ": ", err)
2016-02-03 20:44:20 +00:00
return err
2016-01-28 20:30:05 +00:00
}
this.udpHub = udpHub
}
2016-02-16 13:35:24 +00:00
this.port = port
this.accepting = true
2016-01-27 11:46:40 +00:00
return nil
}
2016-01-30 21:36:28 +00:00
func (this *Shadowsocks) handlerUDPPayload(payload *alloc.Buffer, source v2net.Destination) {
2016-01-28 20:30:05 +00:00
defer payload.Release()
2016-02-23 17:16:13 +00:00
ivLen := this.config.Cipher.IVSize()
iv := payload.Value[:ivLen]
2016-01-28 20:30:05 +00:00
key := this.config.Key
2016-02-23 17:16:13 +00:00
payload.SliceFrom(ivLen)
2016-01-28 20:30:05 +00:00
2016-02-23 17:16:13 +00:00
stream, err := this.config.Cipher.NewDecodingStream(key, iv)
2016-01-28 20:30:05 +00:00
if err != nil {
log.Error("Shadowsocks: Failed to create decoding stream: ", err)
return
}
2016-02-23 17:16:13 +00:00
reader := crypto.NewCryptionReader(stream, payload)
2016-01-29 20:55:42 +00:00
request, err := ReadRequest(reader, NewAuthenticator(HeaderKeyGenerator(key, iv)), true)
2016-01-28 20:30:05 +00:00
if err != nil {
2016-01-30 21:57:20 +00:00
log.Access(source, serial.StringLiteral(""), log.AccessRejected, serial.StringLiteral(err.Error()))
log.Warning("Shadowsocks: Invalid request from ", source, ": ", err)
2016-01-28 20:30:05 +00:00
return
}
2016-05-02 07:38:50 +00:00
//defer request.Release()
2016-01-28 20:30:05 +00:00
2016-01-30 21:36:28 +00:00
dest := v2net.UDPDestination(request.Address, request.Port)
2016-01-30 21:57:20 +00:00
log.Access(source, dest, log.AccessAccepted, serial.StringLiteral(""))
2016-01-30 21:36:28 +00:00
log.Info("Shadowsocks: Tunnelling request to ", dest)
2016-01-30 21:57:20 +00:00
2016-05-01 15:18:02 +00:00
this.udpServer.Dispatch(source, dest, request.DetachUDPPayload(), func(destination v2net.Destination, payload *alloc.Buffer) {
2016-04-25 22:13:26 +00:00
defer payload.Release()
2016-01-28 20:30:05 +00:00
2016-02-23 17:16:13 +00:00
response := alloc.NewBuffer().Slice(0, ivLen)
2016-02-01 20:34:07 +00:00
defer response.Release()
2016-01-28 20:30:05 +00:00
rand.Read(response.Value)
respIv := response.Value
2016-01-28 20:30:05 +00:00
2016-02-23 17:16:13 +00:00
stream, err := this.config.Cipher.NewEncodingStream(key, respIv)
2016-01-28 20:30:05 +00:00
if err != nil {
log.Error("Shadowsocks: Failed to create encoding stream: ", err)
return
}
2016-02-23 17:16:13 +00:00
writer := crypto.NewCryptionWriter(stream, response)
2016-01-28 20:30:05 +00:00
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())
2016-04-25 22:13:26 +00:00
writer.Write(payload.Value)
2016-01-28 20:30:05 +00:00
if request.OTA {
respAuth := NewAuthenticator(HeaderKeyGenerator(key, respIv))
2016-02-23 17:16:13 +00:00
respAuth.Authenticate(response.Value, response.Value[ivLen:])
}
2016-01-30 21:36:28 +00:00
this.udpHub.WriteTo(response.Value, source)
2016-02-01 20:34:07 +00:00
})
2016-01-28 20:30:05 +00:00
}
2016-05-02 21:53:16 +00:00
func (this *Shadowsocks) handleConnection(conn *hub.Connection) {
defer conn.Close()
2016-01-28 11:33:58 +00:00
buffer := alloc.NewSmallBuffer()
defer buffer.Release()
timedReader := v2net.NewTimeOutReader(16, conn)
2016-05-01 15:18:02 +00:00
defer timedReader.Release()
bufferedReader := v2io.NewBufferedReader(timedReader)
defer bufferedReader.Release()
2016-02-23 17:16:13 +00:00
ivLen := this.config.Cipher.IVSize()
2016-05-01 15:18:02 +00:00
_, err := io.ReadFull(bufferedReader, buffer.Value[:ivLen])
2016-01-28 11:33:58 +00:00
if err != nil {
2016-01-30 21:57:20 +00:00
log.Access(conn.RemoteAddr(), serial.StringLiteral(""), log.AccessRejected, serial.StringLiteral(err.Error()))
2016-01-28 11:33:58 +00:00
log.Error("Shadowsocks: Failed to read IV: ", err)
return
}
2016-02-23 17:16:13 +00:00
iv := buffer.Value[:ivLen]
2016-01-28 11:33:58 +00:00
key := this.config.Key
2016-02-23 17:16:13 +00:00
stream, err := this.config.Cipher.NewDecodingStream(key, iv)
2016-01-28 11:33:58 +00:00
if err != nil {
log.Error("Shadowsocks: Failed to create decoding stream: ", err)
return
}
2016-05-01 15:18:02 +00:00
reader := crypto.NewCryptionReader(stream, bufferedReader)
2016-02-23 17:16:13 +00:00
2016-02-28 20:11:50 +00:00
request, err := ReadRequest(reader, NewAuthenticator(HeaderKeyGenerator(key, iv)), false)
2016-01-28 11:33:58 +00:00
if err != nil {
2016-01-30 21:57:20 +00:00
log.Access(conn.RemoteAddr(), serial.StringLiteral(""), log.AccessRejected, serial.StringLiteral(err.Error()))
log.Warning("Shadowsocks: Invalid request from ", conn.RemoteAddr(), ": ", err)
2016-01-28 11:33:58 +00:00
return
}
2016-05-01 15:18:02 +00:00
defer request.Release()
bufferedReader.SetCached(false)
2016-01-28 11:33:58 +00:00
2016-02-03 11:18:28 +00:00
userSettings := protocol.GetUserSettings(this.config.Level)
timedReader.SetTimeOut(userSettings.PayloadReadTimeout)
2016-01-30 21:57:20 +00:00
dest := v2net.TCPDestination(request.Address, request.Port)
log.Access(conn.RemoteAddr(), dest, log.AccessAccepted, serial.StringLiteral(""))
log.Info("Shadowsocks: Tunnelling request to ", dest)
2016-04-25 22:13:26 +00:00
ray := this.packetDispatcher.DispatchToOutbound(dest)
2016-05-07 07:53:15 +00:00
defer ray.InboundOutput().Release()
2016-01-28 11:33:58 +00:00
var writeFinish sync.Mutex
writeFinish.Lock()
go func() {
2016-04-18 16:44:10 +00:00
if payload, err := ray.InboundOutput().Read(); err == nil {
2016-02-23 17:16:13 +00:00
payload.SliceBack(ivLen)
rand.Read(payload.Value[:ivLen])
2016-01-28 12:40:00 +00:00
2016-02-23 17:16:13 +00:00
stream, err := this.config.Cipher.NewEncodingStream(key, payload.Value[:ivLen])
2016-01-31 20:37:00 +00:00
if err != nil {
log.Error("Shadowsocks: Failed to create encoding stream: ", err)
return
}
2016-02-23 17:16:13 +00:00
stream.XORKeyStream(payload.Value[ivLen:], payload.Value[ivLen:])
2016-01-28 12:40:00 +00:00
2016-02-23 17:16:13 +00:00
conn.Write(payload.Value)
2016-01-28 12:40:00 +00:00
payload.Release()
2016-02-23 17:16:13 +00:00
writer := crypto.NewCryptionWriter(stream, conn)
v2writer := v2io.NewAdaptiveWriter(writer)
v2io.Pipe(ray.InboundOutput(), v2writer)
2016-05-01 15:18:02 +00:00
writer.Release()
v2writer.Release()
2016-01-28 12:40:00 +00:00
}
2016-01-28 11:33:58 +00:00
writeFinish.Unlock()
}()
var payloadReader v2io.Reader
if request.OTA {
payloadAuth := NewAuthenticator(ChunkKeyGenerator(iv))
2016-01-29 19:54:06 +00:00
payloadReader = NewChunkReader(reader, payloadAuth)
} else {
payloadReader = v2io.NewAdaptiveReader(reader)
}
2016-04-18 16:44:10 +00:00
v2io.Pipe(payloadReader, ray.InboundInput())
ray.InboundInput().Close()
2016-04-12 19:43:13 +00:00
payloadReader.Release()
2016-01-28 11:33:58 +00:00
writeFinish.Lock()
}
func init() {
internal.MustRegisterInboundHandlerCreator("shadowsocks",
func(space app.Space, rawConfig interface{}) (proxy.InboundHandler, error) {
2016-01-31 16:01:28 +00:00
if !space.HasApp(dispatcher.APP_ID) {
return nil, internal.ErrorBadConfiguration
}
return NewShadowsocks(
rawConfig.(*Config),
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)), nil
2016-01-28 11:33:58 +00:00
})
}