main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2018, Yawning Angel <yawning at torproject dot org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package main
  28. import (
  29. "flag"
  30. "io"
  31. "log"
  32. "net"
  33. "sync"
  34. "git.schwanenlied.me/yawning/shittysocks.git/socks5"
  35. )
  36. func main() {
  37. addr := flag.String("address", "127.0.0.1:1080", "Address to listen on.")
  38. ln, err := net.Listen("tcp", *addr)
  39. if err != nil {
  40. log.Fatalf("Failed to listen: %v", err)
  41. }
  42. log.Printf("Listening on: %v", ln.Addr())
  43. defer ln.Close()
  44. id := 0
  45. for {
  46. conn, err := ln.Accept()
  47. if err != nil {
  48. if e, ok := err.(net.Error); ok && !e.Temporary() {
  49. log.Printf("Critical accept failure: %v", err)
  50. return
  51. }
  52. continue
  53. }
  54. rAddr := conn.RemoteAddr()
  55. log.Printf("Accepted new connection: %v (%d)", rAddr, id)
  56. go connWorker(conn, id)
  57. id++
  58. }
  59. }
  60. func connWorker(conn net.Conn, id int) {
  61. defer conn.Close()
  62. req, err := socks5.Handshake(conn)
  63. if err != nil {
  64. log.Printf("%d: Failed SOCKS5 handshake: %v", id, err)
  65. return
  66. }
  67. log.Printf("%d: Target: %v (Auth: %v:%v)", id, req.Target, req.Uname, req.Passwd)
  68. upConn, err := net.Dial("tcp", req.Target)
  69. if err != nil {
  70. req.Reply(socks5.ErrorToReplyCode(err))
  71. return
  72. }
  73. defer upConn.Close()
  74. if err = req.Reply(socks5.ReplySucceeded); err != nil {
  75. return
  76. }
  77. log.Printf("%d: Connected to target.", id)
  78. if err = copyLoop(conn, upConn); err != nil {
  79. log.Printf("%d: Closed connection: %v", id, err)
  80. } else {
  81. log.Printf("%d: Closed connection", id)
  82. }
  83. }
  84. func copyLoop(a net.Conn, b net.Conn) error {
  85. // Note: b is always the pt connection. a is the SOCKS/ORPort connection.
  86. errChan := make(chan error, 2)
  87. var wg sync.WaitGroup
  88. wg.Add(2)
  89. go func() {
  90. defer wg.Done()
  91. defer b.Close()
  92. defer a.Close()
  93. _, err := io.Copy(b, a)
  94. errChan <- err
  95. }()
  96. go func() {
  97. defer wg.Done()
  98. defer a.Close()
  99. defer b.Close()
  100. _, err := io.Copy(a, b)
  101. errChan <- err
  102. }()
  103. // Wait for both upstream and downstream to close. Since one side
  104. // terminating closes the other, the second error in the channel will be
  105. // something like EINVAL (though io.Copy() will swallow EOF), so only the
  106. // first error is returned.
  107. wg.Wait()
  108. if len(errChan) > 0 {
  109. return <-errChan
  110. }
  111. return nil
  112. }