Moontalk server and client (provided by many parties)
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

98 行
3.3KB

  1. \ Simple torcontrol interface that only supports closing circuits.
  2. \ We only support the authcookie authentication. We can retrieve the authcookie
  3. \ file location by doing the following:
  4. \
  5. \ telnet localhost 9051
  6. \ PROTOCOLINFO
  7. \
  8. \ The user that this server is running under must have permission to read
  9. \ the tor cookie file. On Debian the user must be added to the debian-tor group.
  10. \
  11. \ TODO: write a proper client for this?
  12. \ TODO: at least check for success responses?
  13. \ TODO: we only support ipv4 for now
  14. require unix/socket.fs
  15. require util.4th
  16. require extensions.4th
  17. 512 constant TORCONTROL_SENDBUFFER_SIZE
  18. 512 constant TORCONTROL_RECVBUFFER_SIZE
  19. 32 constant TORCONTROL_COOKIE_FILESIZE
  20. 64 constant TORCONTROL_COOKIE_SIZE
  21. CONFIG_TOR_CONTROL_ADDR constant TORCONTROL_ADDR
  22. CONFIG_TOR_CONTROL_PORT constant TORCONTROL_PORT
  23. CONFIG_TOR_CONTROL_COOKIE_FILEPATH sconstant TORCONTROL_COOKIE_FILEPATH
  24. create torcontrol-cookie TORCONTROL_COOKIE_SIZE allot
  25. create torcontrol-sendbuffer TORCONTROL_SENDBUFFER_SIZE allot
  26. create torcontrol-recvbuffer TORCONTROL_RECVBUFFER_SIZE allot
  27. CONFIG_TOR_CONTROL_AUTHMETHOD TOR_CONTROL_AUTHMETHOD_COOKIE = [IF]
  28. variable (file)
  29. : (zero-prefix) ( c -- str )
  30. 16 < IF s" 0" ELSE 0 0 THEN ;
  31. : (byte>hex) ( c -- str )
  32. hex to-string decimal ;
  33. : (binarycookie>hexcookie) ( binary-str -- )
  34. s" " pad place
  35. over + swap DO
  36. I c@ dup
  37. (zero-prefix) pad +place
  38. (byte>hex) pad +place
  39. LOOP
  40. pad count torcontrol-cookie swap move ;
  41. : torcontrol-load-cookie ( str -- )
  42. r/o open-file throw (file) !
  43. torcontrol-recvbuffer TORCONTROL_COOKIE_FILESIZE (file) @ read-file abort" torcontrol read failed"
  44. TORCONTROL_COOKIE_FILESIZE <> abort" torcontrol read failed."
  45. torcontrol-recvbuffer TORCONTROL_COOKIE_FILESIZE (binarycookie>hexcookie)
  46. (file) @ close-file abort" torcontrol close-file failed" ;
  47. TORCONTROL_COOKIE_FILEPATH torcontrol-load-cookie
  48. [THEN]
  49. variable (tcsocket)
  50. variable (tcsendbuffer-len)
  51. create (tcsaddr) /sockaddr_in alloterase
  52. : (reset) ( -- ) 0 (tcsendbuffer-len) ! ;
  53. : (append) ( str -- )
  54. dup >r torcontrol-sendbuffer (tcsendbuffer-len) @ + swap move
  55. r> (tcsendbuffer-len) +! ;
  56. : (sendbuffer@) ( -- str )
  57. torcontrol-sendbuffer (tcsendbuffer-len) @ ;
  58. : (cookie) ( -- str ) torcontrol-cookie TORCONTROL_COOKIE_SIZE ;
  59. : (lf) ( -- str ) s\" \r\n" ;
  60. : torcontrol-close-circuit ( circuit-id-n -- )
  61. (reset)
  62. CONFIG_TOR_CONTROL_AUTHMETHOD CASE
  63. TOR_CONTROL_AUTHMETHOD_NULL OF
  64. s" AUTHENTICATE " (append) (lf) (append)
  65. ENDOF
  66. TOR_CONTROL_AUTHMETHOD_COOKIE OF
  67. s" AUTHENTICATE " (append) (cookie) (append) (lf) (append)
  68. ENDOF
  69. ." unknown auth method with id " . abort
  70. ENDCASE
  71. s" CLOSECIRCUIT " (append) to-string (append) (lf) (append)
  72. S" QUIT" (append) (lf) (append)
  73. AF_INET SOCK_STREAM 0 socket (tcsocket) !
  74. TORCONTROL_PORT htons (tcsaddr) port w!
  75. TORCONTROL_ADDR (tcsaddr) sin_addr l!
  76. AF_INET (tcsaddr) family w!
  77. (tcsocket) @ (tcsaddr) /sockaddr_in connect 0<> abort" connect failed"
  78. (tcsocket) @ torcontrol-sendbuffer (tcsendbuffer-len) @ 0 send (tcsendbuffer-len) @ <> abort" send failed"
  79. BEGIN
  80. (tcsocket) @ torcontrol-recvbuffer TORCONTROL_RECVBUFFER_SIZE 0 recv
  81. \ dup 0> IF
  82. \ torcontrol-recvbuffer over type
  83. \ THEN
  84. 0=
  85. UNTIL
  86. (tcsocket) @ close 0<> abort" close failed" ;