shadowsocks.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // R.I.P Shadowsocks
  2. package shadowsocks
  3. import (
  4. "github.com/v2ray/v2ray-core/common/log"
  5. v2net "github.com/v2ray/v2ray-core/common/net"
  6. "github.com/v2ray/v2ray-core/proxy"
  7. "github.com/v2ray/v2ray-core/transport/listener"
  8. )
  9. type Shadowsocks struct {
  10. config *Config
  11. port v2net.Port
  12. accepting bool
  13. tcpListener *listener.TCPListener
  14. }
  15. func (this *Shadowsocks) Port() v2net.Port {
  16. return this.port
  17. }
  18. func (this *Shadowsocks) Close() {
  19. this.accepting = false
  20. this.tcpListener.Close()
  21. this.tcpListener = nil
  22. }
  23. func (this *Shadowsocks) Listen(port v2net.Port) error {
  24. if this.accepting {
  25. if this.port == port {
  26. return nil
  27. } else {
  28. return proxy.ErrorAlreadyListening
  29. }
  30. }
  31. tcpListener, err := listener.ListenTCP(port, this.handleConnection)
  32. if err != nil {
  33. log.Error("Shadowsocks: Failed to listen on port ", port, ": ", err)
  34. return err
  35. }
  36. this.tcpListener = tcpListener
  37. this.accepting = true
  38. return nil
  39. }
  40. func (this *Shadowsocks) handleConnection(conn *listener.TCPConn) {
  41. defer conn.Close()
  42. }