door32w.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "net"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "syscall"
  10. "time"
  11. )
  12. const (
  13. connHost = "0.0.0.0"
  14. connPort = "8080"
  15. connType = "tcp"
  16. )
  17. func main() {
  18. var port int
  19. flag.IntVar(&port, "p", 0, "Port number to listen on")
  20. flag.Parse()
  21. if port == 0 && flag.NArg() != 1 {
  22. fmt.Println("I need a Port and a commandline to execute.")
  23. flag.PrintDefaults()
  24. os.Exit(2)
  25. }
  26. fmt.Println("Starting " + connType + " server on " + connHost + ":" + strconv.Itoa(port))
  27. l, err := net.Listen("tcp", "0.0.0.0:"+strconv.Itoa(port))
  28. if err != nil {
  29. fmt.Println("Error listening:", err.Error())
  30. os.Exit(1)
  31. }
  32. defer l.Close()
  33. for {
  34. c, err := l.Accept()
  35. if err != nil {
  36. fmt.Println("Error connecting:", err.Error())
  37. return
  38. }
  39. fmt.Println("Client connected.")
  40. fmt.Println("Client " + c.RemoteAddr().String() + " connected.")
  41. // fork goes here
  42. go handleConnection(c, flag.Arg(0))
  43. c = nil
  44. }
  45. }
  46. func drain(conn net.Conn) {
  47. conn.SetReadDeadline(time.Now().Add(time.Second * 2))
  48. recvData := make([]byte, 32)
  49. n, err := conn.Read(recvData)
  50. if n > 0 {
  51. // do something with recvData[:n]
  52. fmt.Printf(" [%d]\n", n)
  53. }
  54. if err != nil {
  55. fmt.Printf("drain Error: %#v\n", err)
  56. }
  57. conn.SetReadDeadline(time.Time{})
  58. }
  59. func waitForIt(conn net.Conn, pid int) {
  60. process, _ := os.FindProcess(pid)
  61. state, _ := process.Wait()
  62. fmt.Printf("%d State: %#v\n", pid, state)
  63. conn.Write([]byte("\r\nThanks for calling!\r\n"))
  64. conn.Close()
  65. fmt.Println("Connection closed.")
  66. }
  67. func handleConnection(conn net.Conn, cmd string) {
  68. // Something here confuses the crap out of Windows Telnet Client!
  69. conn.Write([]byte("\xff\xfb\x01\xff\xfb\x03\xff\xfd\x10"))
  70. drain(conn)
  71. fmt.Printf("%#v\n", conn)
  72. tcp, _ := conn.(*net.TCPConn)
  73. fmt.Printf("%#v\n", tcp)
  74. // https://github.com/golang/go/issues/10350
  75. raw, err := tcp.SyscallConn()
  76. fmt.Printf("%#v, err: %#v\n", raw, err)
  77. var socket_fd uintptr
  78. raw.Control(func(fd uintptr) {
  79. socket_fd = fd
  80. })
  81. fmt.Printf("Socket: %#v\n", socket_fd)
  82. proc, _ := syscall.GetCurrentProcess()
  83. var dup_socket_fd syscall.Handle
  84. err = syscall.DuplicateHandle(proc, syscall.Handle(socket_fd),
  85. proc, &dup_socket_fd, syscall.DUPLICATE_SAME_ACCESS, true, 0)
  86. if err != nil {
  87. fmt.Printf("ERR DuplicateHandle: %#v\n", err)
  88. }
  89. fmt.Printf("Dup: %#v\n", dup_socket_fd)
  90. // windows: tcp.File() failes. net.OpError
  91. /*
  92. file, err := tcp.File()
  93. fd := file.Fd()
  94. fmt.Printf("FD %#v, %#v\n", fd, err)
  95. */
  96. /*
  97. tcp, _ := conn.(*net.TCPConn)
  98. file, _ := tcp.File()
  99. fd := file.Fd()
  100. */
  101. // what we actually put into the file
  102. filefd := int64(dup_socket_fd)
  103. // fdstr := strconv.Itoa(int(filefd))
  104. fp, _ := os.Create("door32.sys")
  105. fmt.Fprintf(fp, "2\n%d\n38400\nFake Door32\n1\nBugz Laundry\nBugz\n100\n120\n1\n1\n", filefd)
  106. fp.Close()
  107. parts := strings.Split(cmd, " ")
  108. // parts = append(parts, fdstr)
  109. // id, _ := syscall.ForkExec(parts[0], parts[1:], nil)
  110. fmt.Printf("Running: [%s] with %#v\n", parts[0], parts[1:])
  111. // https://stackoverflow.com/questions/35336131/createprocess-with-golang
  112. var si syscall.StartupInfo
  113. var pi syscall.ProcessInformation
  114. argv := syscall.StringToUTF16Ptr(cmd)
  115. err = syscall.CreateProcess(nil, argv, nil, nil, true, 0, nil, nil, &si, &pi)
  116. fmt.Printf("Return: %#v\n", err)
  117. raw = nil
  118. if err == nil {
  119. event, e := syscall.WaitForSingleObject(pi.Process, syscall.INFINITE)
  120. fmt.Printf("Event %#v, err: %#v\n", event, e)
  121. }
  122. /*
  123. exec_cmd := exec.Command(parts[0], parts[1:]...)
  124. fmt.Printf("exec_cmd: %#v\n", exec_cmd)
  125. err = exec_cmd.Run()
  126. if err != nil {
  127. fmt.Println("Error: ", err)
  128. }
  129. fmt.Println("Command completed.")
  130. // UH, WHAT? I didn't see that it started, and it certainly didn't keep running. :(
  131. */
  132. // id := 13
  133. /*
  134. id, _ := syscall.ForkExec(parts[0], parts,
  135. &syscall.ProcAttr{
  136. Env: os.Environ(),
  137. Sys: &syscall.SysProcAttr{
  138. Setsid: true,
  139. },
  140. Files: []uintptr{0, 1, 2, fd}, // print message to the same pty
  141. })
  142. */
  143. // go waitForIt(conn, id)
  144. // id, _, _ := syscall.Syscall(syscall.SYS_FORK, 0, 0, 0)
  145. /*
  146. if id == 0 {
  147. // child process
  148. exec.Command(parts[0], parts[1:]...)
  149. os.Exit(2)
  150. }
  151. */
  152. // fmt.Printf("Child started: %d\n", id)
  153. // return
  154. conn.Write([]byte("Welcome back!\r\nSeeya!\r\n"))
  155. // I can't seem to get this to close the connection. :(
  156. conn.Close()
  157. syscall.CloseHandle(syscall.Handle(socket_fd))
  158. tcp.SetLinger(0)
  159. tcp.Close()
  160. /*
  161. for {
  162. buffer, err := bufio.NewReader(conn).ReadBytes('\r') // HMM. \r ?
  163. if err != nil {
  164. fmt.Println("Client left.")
  165. conn.Close()
  166. return
  167. }
  168. // What am I seeing here?
  169. log.Println("Client message:", string(buffer[:len(buffer)-1]))
  170. conn.Write(buffer)
  171. conn.Write([]byte("\n"))
  172. }
  173. */
  174. }