2016-01-27 11:46:40 +00:00
|
|
|
// R.I.P Shadowsocks
|
|
|
|
|
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
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"
|
|
|
|
"github.com/v2ray/v2ray-core/transport/listener"
|
2016-01-27 11:46:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Shadowsocks struct {
|
2016-01-27 21:11:31 +00:00
|
|
|
config *Config
|
|
|
|
port v2net.Port
|
|
|
|
accepting bool
|
|
|
|
tcpListener *listener.TCPListener
|
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
|
|
|
|
this.tcpListener.Close()
|
|
|
|
this.tcpListener = nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 11:46:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-01-27 21:11:31 +00:00
|
|
|
|
|
|
|
func (this *Shadowsocks) handleConnection(conn *listener.TCPConn) {
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
}
|