main.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. "os"
  34. "os/signal"
  35. "strings"
  36. "sync"
  37. "git.schwanenlied.me/yawning/shittysocks.git/socks5"
  38. )
  39. func main() {
  40. const (
  41. netUnix = "unix"
  42. prefixUnix = netUnix + ":"
  43. )
  44. pAddr := flag.String("address", "127.0.0.1:1080", "Address to listen on.")
  45. flag.Parse()
  46. addr, netwerk := *pAddr, "tcp"
  47. if strings.HasPrefix(addr, prefixUnix) {
  48. netwerk = netUnix
  49. addr = strings.TrimPrefix(addr, prefixUnix)
  50. }
  51. ln, err := net.Listen(netwerk, addr)
  52. if err != nil {
  53. log.Fatalf("Failed to listen: %v", err)
  54. }
  55. log.Printf("Listening on: %v", ln.Addr())
  56. defer func() {
  57. if netwerk == netUnix {
  58. os.Remove(addr)
  59. }
  60. ln.Close()
  61. }()
  62. ch := make(chan os.Signal, 1)
  63. signal.Notify(ch, os.Interrupt)
  64. go func() {
  65. id := 0
  66. for {
  67. conn, err := ln.Accept()
  68. if err != nil {
  69. if e, ok := err.(net.Error); ok && !e.Temporary() {
  70. log.Printf("Critical accept failure: %v", err)
  71. return
  72. }
  73. continue
  74. }
  75. rAddr := conn.RemoteAddr()
  76. log.Printf("Accepted new connection: %v (%d)", rAddr, id)
  77. go connWorker(conn, id)
  78. id++
  79. }
  80. }()
  81. <-ch
  82. log.Printf("Terminating.")
  83. }
  84. func connWorker(conn net.Conn, id int) {
  85. defer conn.Close()
  86. req, err := socks5.Handshake(conn)
  87. if err != nil {
  88. log.Printf("%d: Failed SOCKS5 handshake: %v", id, err)
  89. return
  90. }
  91. log.Printf("%d: Target: %v (Auth: `%v`/`%v`)", id, req.Target, string(req.Uname), string(req.Passwd))
  92. upConn, err := net.Dial("tcp", req.Target)
  93. if err != nil {
  94. req.Reply(socks5.ErrorToReplyCode(err))
  95. return
  96. }
  97. defer upConn.Close()
  98. if err = req.Reply(socks5.ReplySucceeded); err != nil {
  99. return
  100. }
  101. log.Printf("%d: Connected to target.", id)
  102. if err = copyLoop(conn, upConn); err != nil {
  103. log.Printf("%d: Closed connection: %v", id, err)
  104. } else {
  105. log.Printf("%d: Closed connection", id)
  106. }
  107. }
  108. func copyLoop(a net.Conn, b net.Conn) error {
  109. // Note: b is always the pt connection. a is the SOCKS/ORPort connection.
  110. errChan := make(chan error, 2)
  111. var wg sync.WaitGroup
  112. wg.Add(2)
  113. go func() {
  114. defer wg.Done()
  115. defer b.Close()
  116. defer a.Close()
  117. _, err := io.Copy(b, a)
  118. errChan <- err
  119. }()
  120. go func() {
  121. defer wg.Done()
  122. defer a.Close()
  123. defer b.Close()
  124. _, err := io.Copy(a, b)
  125. errChan <- err
  126. }()
  127. // Wait for both upstream and downstream to close. Since one side
  128. // terminating closes the other, the second error in the channel will be
  129. // something like EINVAL (though io.Copy() will swallow EOF), so only the
  130. // first error is returned.
  131. wg.Wait()
  132. if len(errChan) > 0 {
  133. return <-errChan
  134. }
  135. return nil
  136. }