Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.95 KB | None | 0 0
  1. ; <COMPILER: v1.1.08.01>
  2. WS_HandleEvents(socket, events="READ ACCEPT CONNECT CLOSE")
  3. {
  4. static FD_READ := 1, FD_ACCEPT := 8, FD_CONNECT := 16, FD_CLOSE := 32
  5. static msg := 0xB93D
  6. Ptr := (A_PtrSize) ? "uptr" : "uint"
  7. fdevents := 0
  8. Loop, parse, events, % " "
  9. {
  10. if (A_LoopField="READ")
  11. fdevents |= FD_READ
  12. else if (A_LoopField="ACCEPT")
  13. fdevents |= FD_ACCEPT
  14. else if (A_LoopField="CONNECT")
  15. fdevents |= FD_CONNECT
  16. else if (A_LoopField="CLOSE")
  17. fdevents |= FD_CLOSE
  18. }
  19. WS_Log(A_ThisFunc "(" socket ")", 3)
  20. OnMessage(msg, "WS_Proc")
  21. Gui, 33: +LastFound
  22. if (DllCall("ws2_32\WSAAsyncSelect", Ptr, socket, Ptr, WinExist(), "uint", msg, "uint", fdevents)!=0)
  23. {
  24. WS_Log("[ERROR] Can't handle events", 1)
  25. return 0
  26. }
  27. return 1
  28. }
  29. WS_Proc(wParam, lParam, msg, hwnd)
  30. {
  31. static FD_READ := 1, FD_ACCEPT := 8, FD_CONNECT := 16, FD_CLOSE := 32
  32. event := lParam & 0xFFFF
  33. error := lParam >> 16
  34. socket := wParam
  35. if (event=FD_READ)
  36. {
  37. Func := (IsFunc("WS_OnRead")) ? "WS_OnRead" : "WS_DefProc"
  38. if (IsFunc("WS_OnRead"))
  39. result := %Func%(socket)
  40. if ((result=0) || (!IsFunc("WS_OnRead")))
  41. WS_DefProc(socket, event)
  42. }
  43. else if (event=FD_ACCEPT)
  44. {
  45. Func := (IsFunc("WS_OnAccept")) ? "WS_OnAccept" : "WS_DefProc"
  46. if (IsFunc("WS_OnAccept"))
  47. result := %Func%(socket)
  48. if ((result=0) || (!IsFunc("WS_OnAccept")))
  49. WS_DefProc(socket, event)
  50. }
  51. else if (event=FD_CONNECT)
  52. {
  53. Func := (IsFunc("WS_OnConnect")) ? "WS_OnConnect" : "WS_DefProc"
  54. if (IsFunc("WS_OnConnect"))
  55. result := %Func%(socket)
  56. if ((result=0) || (!IsFunc("WS_OnConnect")))
  57. WS_DefProc(socket, event)
  58. }
  59. else if (event=FD_CLOSE)
  60. {
  61. Func := (IsFunc("WS_OnClose")) ? "WS_OnClose" : "WS_DefProc"
  62. if (IsFunc("WS_OnClose"))
  63. result := %Func%(socket)
  64. if ((result=0) || (!IsFunc("WS_OnClose")))
  65. WS_DefProc(socket, event)
  66. }
  67. return 1
  68. }
  69. WS_DefProc(socket, event)
  70. {
  71. global WS_LASTRECVMESSAGE, WS_LASTACCEPTEDSOCKET, WS_LASTACCEPTEDIP, WS_LASTACCEPTEDPORT
  72. global WS_LASTCONNECTEDSOCKET, WS_LASTCLOSEDSOCKET
  73. static FD_READ := 1, FD_ACCEPT := 8, FD_CONNECT := 16, FD_CLOSE := 32
  74. if (event=FD_READ)
  75. {
  76. WS_Log("Handle READ for socket " socket, 5)
  77. size := WS_MessageSize(socket)
  78. if (size>0)
  79. WS_Recv(socket, WS_LASTRECVMESSAGE, size)
  80. return 1
  81. }
  82. else if (event=FD_ACCEPT)
  83. {
  84. WS_Log("Handle ACCEPT for socket " socket, 5)
  85. WS_LASTACCEPTEDSOCKET := WS_Accept(socket, WS_LASTACCEPTEDIP, WS_LASTACCEPTEDPORT)
  86. return 1
  87. }
  88. else if (event=FD_CONNECT)
  89. {
  90. WS_Log("Handle CONNECT for socket " socket, 5)
  91. WS_LASTCONNECTEDSOCKET := socket
  92. return 1
  93. }
  94. else if (event=FD_CLOSE)
  95. {
  96. WS_Log("Handle CLOSE for socket " socket, 5)
  97. WS_LASTCLOSEDSOCKET := socket
  98. WS_CloseSocket(socket)
  99. return 1
  100. }
  101. return 1
  102. }
  103. WS_MessageSize(socket)
  104. {
  105. static FIONREAD := 0x4004667F
  106. Ptr := (A_PtrSize) ? "uptr" : "uint"
  107. VarSetCapacity(argp, 4, 0)
  108. if (DllCall("ws2_32\ioctlsocket", Ptr, socket, "uint", FIONREAD, Ptr, &argp)!=0)
  109. return 0
  110. return NumGet(argp, 0, "int")
  111. }
  112. WS_EnableBroadcast(socket)
  113. {
  114. static SOL_SOCKET := 0xFFFF
  115. static SO_BROADCAST := 0x0020
  116. static IPPROTO_UDP := 17
  117. Ptr := (A_PtrSize) ? "uptr" : "uint"
  118. WS_Log(A_ThisFunc "(" socket ")", 3)
  119. if (WS_GetSocketInfo(socket, af, maxsockaddr, minsockaddr, type, protocol)!=0)
  120. {
  121. WS_Log("[ERROR] Can't retreview information of this socket!", 1)
  122. return 0
  123. }
  124. if (protocol!=IPPROTO_UDP)
  125. {
  126. WS_Log("[ERROR] Can't set the broadcast-state for non UDP-sockets!", 1)
  127. return 0
  128. }
  129. VarSetCapacity(optval, 4, 0)
  130. NumPut(1, optval, 0, "uint")
  131. if (DllCall("ws2_32\setsockopt", Ptr, socket, "int", SOL_SOCKET, "int", SO_BROADCAST, Ptr, &optval, "int", 4)!=0)
  132. {
  133. WS_Log("[ERROR] Can't set the broadcast-state!", 1)
  134. return 0
  135. }
  136. WS_Log("Broadcast-state enabled!")
  137. return 1
  138. }
  139. WS_GetAddrInfo(socket, hostname_or_ip, port, byref sockaddr, byref sockaddrlen)
  140. {
  141. static AF_INET := 2, AF_INET6 := 23
  142. static ADDR_ANY := 0xFFFFFFFF, ADDR_NONE := 0
  143. static addr
  144. sPtr := (A_PtrSize) ? A_PtrSize : 4
  145. Ptr := (A_PtrSize) ? "uptr" : "uint"
  146. AStr := (A_IsUnicode) ? "astr" : "str"
  147. if (WS_GetSocketInfo(socket, af, maxsockaddr, minsockaddr, type, protocol)!=0)
  148. {
  149. WS_Log("[ERROR] Can't retreview information of this socket!", 1)
  150. return 0
  151. }
  152. VarSetCapacity(hints, 20+(3*sPtr), 0)
  153. NumPut(af, hints, 4, "int")
  154. NumPut(type, hints, 8, "int")
  155. NumPut(protocol, hints, 12, "int")
  156. if (DllCall("ws2_32\getaddrinfo", AStr, hostname_or_ip, AStr, port, Ptr, &hints, Ptr "*", result)!=0)
  157. {
  158. WS_Log("[ERROR] Can't retreview information of this address!", 1)
  159. return 0
  160. }
  161. sockaddrlen := NumGet(result+0, 16, "int")
  162. sockaddr := NumGet(result+0, 16+(2*sPtr), Ptr)
  163. if (af=AF_INET)
  164. {
  165. ip := NumGet(sockaddr+0, 4, "uchar") "." NumGet(sockaddr+0, 5, "uchar") "." NumGet(sockaddr+0, 6, "uchar") "." NumGet(sockaddr+0, 7, "uchar")
  166. if (ip!=hostname_or_ip)
  167. WS_Log("IP converted from " hostname_or_ip " to " ip ":" DllCall("ws2_32\ntohs", "ushort", NumGet(sockaddr+0, 2, "ushort"), "ushort"))
  168. }
  169. return 1
  170. }
  171. WS_Send(socket, message, len=0, flags=0)
  172. {
  173. static MSG_DONTROUTE := 4, MSG_OOB := 1
  174. Ptr := (A_PtrSize) ? "uptr" : "uint"
  175. AStr := (A_IsUnicode) ? "astr" : "str"
  176. if (len<=0)
  177. len := strlen(message)
  178. fl := 0
  179. Loop, parse, flags, % " "
  180. {
  181. if (A_LoopField="MSG_DONTROUTE")
  182. fl |= MSG_DONTROUTE
  183. else if (A_LoopField="MSG_OOB")
  184. fl |= MSG_OOB
  185. }
  186. WS_Log(A_ThisFunc "(" socket ", message, " len ", " flags ")", 3)
  187. result := DllCall("ws2_32\send", Ptr, socket, AStr, message, "int", len, "int", fl, "int")
  188. if (result=-1)
  189. {
  190. WS_Log("[ERROR] Sending failed!", 1)
  191. return 0
  192. }
  193. WS_Log(result " of " len " bytes sent.`n----`nSending Message:`n----`n" Substr(message, 1, len) "`n----", 4)
  194. return result
  195. }
  196. WS_SendBinary(socket, pbuffer, len, flags=0)
  197. {
  198. static MSG_DONTROUTE := 4, MSG_OOB := 1
  199. Ptr := (A_PtrSize) ? "uptr" : "uint"
  200. AStr := (A_IsUnicode) ? "astr" : "str"
  201. fl := 0
  202. Loop, parse, flags, % " "
  203. {
  204. if (A_LoopField="MSG_DONTROUTE")
  205. fl |= MSG_DONTROUTE
  206. else if (A_LoopField="MSG_OOB")
  207. fl |= MSG_OOB
  208. }
  209. WS_Log(A_ThisFunc "(" socket ", message, " len ", " flags ")", 3)
  210. result := DllCall("ws2_32\send", Ptr, socket, Ptr, pbuffer, "int", len, "int", fl, "int")
  211. if (result=-1)
  212. {
  213. WS_Log("[ERROR] Sending failed!", 1)
  214. return 0
  215. }
  216. WS_Log(result " of " len " bytes sent (binary)", 4)
  217. return result
  218. }
  219. WS_SendFile(socket, file, flags=0)
  220. {
  221. static MSG_DONTROUTE := 4, MSG_OOB := 1
  222. static GENERIC_READ := 0x80000000, OPEN_EXISTING := 3
  223. Ptr := (A_PtrSize) ? "uptr" : "uint"
  224. AStr := (A_IsUnicode) ? "astr" : "str"
  225. fl := 0
  226. Loop, parse, flags, % " "
  227. {
  228. if (A_LoopField="MSG_DONTROUTE")
  229. fl |= MSG_DONTROUTE
  230. else if (A_LoopField="MSG_OOB")
  231. fl |= MSG_OOB
  232. }
  233. if ((hFile := DllCall("CreateFile", Ptr, &file, "uint", GENERIC_READ, "uint", 0, Ptr, 0, "uint", OPEN_EXISTING, "uint", 0, Ptr, 0, Ptr))=0)
  234. {
  235. WS_Log("[ERROR] Can't open the file!", 1)
  236. return 0
  237. }
  238. if ((len := DllCall("GetFileSize", Ptr, hFile, Ptr, 0, "uint"))=0xFFFFFFFF)
  239. {
  240. WS_Log("[ERROR] Can't get the filesize!", 1)
  241. return 0
  242. }
  243. VarSetCapacity(buffer, len)
  244. if (DllCall("ReadFile", Ptr, hFile, Ptr, &buffer, "uint", len, Ptr, 0, Ptr, 0)=0)
  245. {
  246. WS_Log("[ERROR] Can't read from the file!", 1)
  247. return 0
  248. }
  249. if (DllCall("CloseHandle", Ptr, hFile)=0)
  250. {
  251. WS_Log("[ERROR] Can't close the file!", 1)
  252. return 0
  253. }
  254. WS_Log(A_ThisFunc "(" socket ", " file ", " flags ")", 3)
  255. result := DllCall("ws2_32\send", Ptr, socket, Ptr, &buffer, "int", len, "int", fl, "int")
  256. if (result=-1)
  257. {
  258. WS_Log("[ERROR] Sending failed!", 1)
  259. return 0
  260. }
  261. WS_Log(result " of " len " bytes sent from " file, 4)
  262. return result
  263. }
  264. WS_SendTo(socket, ip, port, message, len=0, flags=0)
  265. {
  266. static MSG_DONTROUTE := 4, MSG_OOB := 1
  267. Ptr := (A_PtrSize) ? "uptr" : "uint"
  268. AStr := (A_IsUnicode) ? "astr" : "str"
  269. if (len<=0)
  270. len := strlen(message)
  271. fl := 0
  272. Loop, parse, flags, % " "
  273. {
  274. if (A_LoopField="MSG_DONTROUTE")
  275. fl |= MSG_DONTROUTE
  276. else if (A_LoopField="MSG_OOB")
  277. fl |= MSG_OOB
  278. }
  279. WS_Log(A_ThisFunc "(" socket ", " ip ", " port ", message, " len ", " flags ")", 3)
  280. if (!WS_GetAddrInfo(socket, ip, port, sockaddr, sockaddrlen))
  281. {
  282. WS_Log("Can't get an addressinfo!", 1)
  283. return 0
  284. }
  285. result := DllCall("ws2_32\sendto", Ptr, socket, AStr, message, "int", len, "int", fl, Ptr, sockaddr, "int", sockaddrlen, "int")
  286. if (result=-1)
  287. {
  288. WS_Log("[ERROR] Sending failed!", 1)
  289. return 0
  290. }
  291. WS_Log(result " of " len " bytes sent.`n----`nSending Message:`n----`n" Substr(message, 1, len) "`n----", 4)
  292. return result
  293. }
  294. WS_Recv(socket, byref message, len=0, flags=0)
  295. {
  296. static MSG_PEEK := 2, MSG_OOB := 1, MSG_WAITALL := 8
  297. Ptr := (A_PtrSize) ? "uptr" : "uint"
  298. fl := 0
  299. Loop, parse, flags, % " "
  300. {
  301. if (A_LoopField="MSG_PEEK")
  302. fl |= MSG_PEEK
  303. else if (A_LoopField="MSG_OOB")
  304. fl |= MSG_OOB
  305. else if (A_LoopField="MSG_WAITALL")
  306. fl |= MSG_WAITALL
  307. }
  308. WS_Log(A_ThisFunc "(" socket ", message, " len ", " flags ")", 3)
  309. while ((size := WS_MessageSize(socket))=0)
  310. sleep, 20
  311. if (len=0)
  312. len := size
  313. VarSetCapacity(buffer, len)
  314. result := DllCall("ws2_32\recv", Ptr, socket, Ptr, &buffer, "int", len, "int", fl)
  315. if (result=-1)
  316. {
  317. WS_Log("[ERROR] Receiving failed!", 1)
  318. return 0
  319. }
  320. message := ""
  321. Loop, % result
  322. message .= Chr(NumGet(buffer, A_Index-1, "uchar"))
  323. message .= Chr(0)
  324. WS_Log(result " bytes received.`n----`nReceived Message:`n----`n" message "`n----", 4)
  325. return result
  326. }
  327. WS_RecvBinary(socket, byref pbuffer, len, flags=0)
  328. {
  329. static MSG_PEEK := 2, MSG_OOB := 1, MSG_WAITALL := 8
  330. Ptr := (A_PtrSize) ? "uptr" : "uint"
  331. fl := 0
  332. Loop, parse, flags, % " "
  333. {
  334. if (A_LoopField="MSG_PEEK")
  335. fl |= MSG_PEEK
  336. else if (A_LoopField="MSG_OOB")
  337. fl |= MSG_OOB
  338. else if (A_LoopField="MSG_WAITALL")
  339. fl |= MSG_WAITALL
  340. }
  341. WS_Log(A_ThisFunc "(" socket ", message, " len ", " flags ")", 3)
  342. result := DllCall("ws2_32\recv", Ptr, socket, Ptr, pbuffer, "int", len, "int", fl)
  343. if (result=-1)
  344. {
  345. WS_Log("[ERROR] Receiving failed!", 1)
  346. return 0
  347. }
  348. message := ""
  349. Loop, % result
  350. message .= Chr(NumGet(buffer, A_Index-1, "uchar"))
  351. message .= Chr(0)
  352. WS_Log(result " bytes received (binary)", 4)
  353. return result
  354. }
  355. WS_RecvFile(socket, file, flags=0)
  356. {
  357. static MSG_PEEK := 2, MSG_OOB := 1, MSG_WAITALL := 8
  358. static GENERIC_WRITE := 0x40000000, CREATE_ALWAYS := 2
  359. Ptr := (A_PtrSize) ? "uptr" : "uint"
  360. fl := 0
  361. Loop, parse, flags, % " "
  362. {
  363. if (A_LoopField="MSG_PEEK")
  364. fl |= MSG_PEEK
  365. else if (A_LoopField="MSG_OOB")
  366. fl |= MSG_OOB
  367. else if (A_LoopField="MSG_WAITALL")
  368. fl |= MSG_WAITALL
  369. }
  370. while ((len := WS_MessageSize(socket))=0)
  371. sleep, 20
  372. VarSetCapacity(buffer, len)
  373. WS_Log(A_ThisFunc "(" socket ", " file ", " flags ")", 3)
  374. result := DllCall("ws2_32\recv", Ptr, socket, Ptr, &buffer, "int", len, "int", fl)
  375. if (result=-1)
  376. {
  377. WS_Log("[ERROR] Receiving failed!", 1)
  378. return 0
  379. }
  380. if ((hFile := DllCall("CreateFile", Ptr, &file, "uint", GENERIC_WRITE, "uint", 0, Ptr, 0, "uint", CREATE_ALWAYS, "uint", 0, Ptr, 0, Ptr))=0)
  381. {
  382. WS_Log("[ERROR] Can't create the file!", 1)
  383. return 0
  384. }
  385. if (DllCall("WriteFile", Ptr, hFile, Ptr, &buffer, "uint", len, Ptr, 0, Ptr, 0)=0)
  386. {
  387. WS_Log("[ERROR] Can't write into the file!", 1)
  388. return 0
  389. }
  390. if (DllCall("CloseHandle", Ptr, hFile)=0)
  391. {
  392. WS_Log("[ERROR] Can't close the file!", 1)
  393. return 0
  394. }
  395. WS_Log(result " bytes received and saved in " file, 4)
  396. return result
  397. }
  398. WS_RecvFrom(socket, byref out_ip, byref out_port, byref message, len=0, flags=0)
  399. {
  400. static MSG_PEEK := 2, MSG_OOB := 1, MSG_WAITALL := 8
  401. Ptr := (A_PtrSize) ? "uptr" : "uint"
  402. fl := 0
  403. Loop, parse, flags, % " "
  404. {
  405. if (A_LoopField="MSG_PEEK")
  406. fl |= MSG_PEEK
  407. else if (A_LoopField="MSG_OOB")
  408. fl |= MSG_OOB
  409. else if (A_LoopField="MSG_WAITALL")
  410. fl |= MSG_WAITALL
  411. }
  412. WS_Log(A_ThisFunc "(" socket ", message, " len ", " flags ")", 3)
  413. while ((size := WS_MessageSize(socket))=0)
  414. sleep, 20
  415. if (len=0)
  416. len := size
  417. VarSetCapacity(buffer, len)
  418. if (WS_GetSocketInfo(socket, af, maxsockaddr, minsockaddr, type, protocol)!=0)
  419. {
  420. WS_Log("[ERROR] Can't retreview information of this socket!", 1)
  421. return 0
  422. }
  423. VarSetCapacity(sockaddr, maxsockaddr, 0)
  424. result := DllCall("ws2_32\recv", Ptr, socket, Ptr, &buffer, "int", len, "int", fl, Ptr, &sockaddr, "int", maxsockaddr)
  425. if (result=-1)
  426. {
  427. WS_Log("[ERROR] Receiving failed!", 1)
  428. return 0
  429. }
  430. message := ""
  431. Loop, % result
  432. message .= Chr(NumGet(buffer, A_Index-1, "uchar"))
  433. message .= Chr(0)
  434. out_port := DllCall("ws2_32\htons", "ushort", NumGet(sockaddr, 2, "ushort"))
  435. out_ip := ""
  436. if (NumGet(sockaddr, 0, "short")=AF_INET)
  437. {
  438. pout_ip := DllCall("ws2_32\inet_ntoa", "uint", NumGet(sockaddr, 4, "uint"))
  439. loop, 32
  440. {
  441. if (NumGet(pout_ip+0, A_Index-1, "uchar")=0)
  442. break
  443. out_ip .= Chr(NumGet(pout_ip+0, A_Index-1, "uchar"))
  444. }
  445. }
  446. else if (NumGet(sockaddr, 0, "short")=AF_INET6)
  447. {
  448. VarSetCapacity(out_ip, 64)
  449. DllCall("ws2_32\InetNtop", "int", AF_INET6, Ptr, &sockaddr+12, Ptr, &out_ip, int, 64)
  450. }
  451. WS_Log(result " bytes received.`n----`nReceived Message:`n----`n" message "`n----", 4)
  452. return result
  453. }
  454. WS_Connect(socket, ip, port)
  455. {
  456. static AF_INET := 2, AF_INET6 := 23
  457. WS_Log(A_ThisFunc "(" socket ", " ip ", " port ")", 3)
  458. sPtr := (A_PtrSize) ? A_PtrSize : 4
  459. Ptr := (A_PtrSize) ? "uptr" : "uint"
  460. AStr := (A_IsUnicode) ? "astr" : "str"
  461. if (!WS_GetAddrInfo(socket, ip, port, sockaddr, sockaddrlen))
  462. {
  463. WS_Log("Can't get an addressinfo!", 1)
  464. return 0
  465. }
  466. if (DllCall("ws2_32\WSAConnect", Ptr, socket, Ptr, sockaddr, int, sockaddrlen, Ptr, 0, Ptr, 0, Ptr, 0, Ptr, 0, "int")!=0)
  467. {
  468. WS_Log("[ERROR] Can't connect to this address!", 1)
  469. return 0
  470. }
  471. WS_Log("Connected to " ip " on Port " port ".")
  472. return 1
  473. }
  474. WS_Accept(socket, byref out_ip, byref out_port)
  475. {
  476. static AF_INET := 2, AF_INET6 := 23
  477. WS_Log(A_ThisFunc "(" socket ")", 3)
  478. sPtr := (A_PtrSize) ? A_PtrSize : 4
  479. Ptr := (A_PtrSize) ? "uptr" : "uint"
  480. AStr := (A_IsUnicode) ? "astr" : "str"
  481. if (WS_GetSocketInfo(socket, af, maxsockaddr, minsockaddr, type, protocol)!=0)
  482. {
  483. WS_Log("[ERROR] Can't retreview information of this socket!", 1)
  484. return 0
  485. }
  486. VarSetCapacity(sockaddr, maxsockaddr, 0)
  487. WS_Log("Waiting for connection to accept.")
  488. VarSetCapacity(pmaxsockaddr, 4, 0)
  489. NumPut(maxsockaddr, pmaxsockaddr)
  490. newsocket := DllCall("ws2_32\accept", Ptr, socket, Ptr, &sockaddr, Ptr, &pmaxsockaddr)
  491. if (newsocket=-1)
  492. {
  493. WS_Log("[ERROR] Can't accept connections!", 1)
  494. return 0
  495. }
  496. out_port := DllCall("ws2_32\htons", "ushort", NumGet(sockaddr, 2, "ushort"))
  497. out_ip := ""
  498. if (NumGet(sockaddr, 0, "short")=AF_INET)
  499. {
  500. pout_ip := DllCall("ws2_32\inet_ntoa", "uint", NumGet(sockaddr, 4, "uint"))
  501. loop, 32
  502. {
  503. if (NumGet(pout_ip+0, A_Index-1, "uchar")=0)
  504. break
  505. out_ip .= Chr(NumGet(pout_ip+0, A_Index-1, "uchar"))
  506. }
  507. }
  508. else if (NumGet(sockaddr, 0, "short")=AF_INET6)
  509. {
  510. VarSetCapacity(out_ip, 64)
  511. DllCall("ws2_32\InetNtop", "int", AF_INET6, Ptr, &sockaddr+12, Ptr, &out_ip, int, 64)
  512. }
  513. WS_Log("Connection from " out_ip " accepted on Port " out_port ".")
  514. return newsocket
  515. }
  516. WS_Bind(socket, ip, port)
  517. {
  518. static AF_INET := 2, AF_INET6 := 23
  519. WS_Log(A_ThisFunc "(" socket ", " ip ", " port ")", 3)
  520. sPtr := (A_PtrSize) ? A_PtrSize : 4
  521. Ptr := (A_PtrSize) ? "uptr" : "uint"
  522. AStr := (A_IsUnicode) ? "astr" : "str"
  523. if (!WS_GetAddrInfo(socket, ip, port, sockaddr, sockaddrlen))
  524. {
  525. WS_Log("Can't get an addressinfo!", 1)
  526. return 0
  527. }
  528. if (DllCall("ws2_32\bind", Ptr, socket, Ptr, sockaddr, "int", sockaddrlen)!=0)
  529. {
  530. WS_Log("[ERROR] Can't bind to this address!", 1)
  531. return 0
  532. }
  533. WS_Log("Socket bound to " ip " on Port " port ".")
  534. return 1
  535. }
  536. WS_Listen(socket, backlog=32)
  537. {
  538. WS_Log(A_ThisFunc "(" socket ", " backlog ")", 3)
  539. Ptr := (A_PtrSize) ? "uptr" : "uint"
  540. if (DllCall("ws2_32\listen", Ptr, socket, "int", backlog)!=0)
  541. {
  542. WS_Log("[ERROR] Can't listen on this socket!", 1)
  543. return 0
  544. }
  545. WS_Log("The socket is listening.")
  546. return 1
  547. }
  548. WS_GetSocketInfo(socket, byref af, byref maxsockaddr, byref minsockaddr, byref type, byref protocol)
  549. {
  550. static SOL_SOCKET := 0xFFFF
  551. static SO_PROTOCOL_INFOA := 0x2004
  552. sPtr := (A_PtrSize) ? A_PtrSize : 4
  553. Ptr := (A_PtrSize) ? "uptr" : "uint"
  554. size := 1024
  555. VarSetCapacity(protocol_info, size, 0)
  556. VarSetCapacity(psize, 4, 0)
  557. NumPut(size, psize)
  558. result := DllCall("ws2_32\getsockopt", Ptr, socket, "int", SOL_SOCKET, "int", SO_PROTOCOL_INFOA, Ptr, &protocol_info, Ptr, &psize)
  559. offset := 76
  560. af := NumGet(protocol_info, offset, "int")
  561. maxsockaddr := NumGet(protocol_info, offset+4, "int")
  562. minsockaddr := NumGet(protocol_info, offset+8, "int")
  563. type := NumGet(protocol_info, offset+12, "int")
  564. protocol := NumGet(protocol_info, offset+16, "int")
  565. return result
  566. }
  567. WS_Socket(protocol="TCP", ipversion="IPv4")
  568. {
  569. static AF_INET := 2, AF_INET6 := 23
  570. static SOCK_STREAM := 1, SOCK_DGRAM := 2
  571. static IPPROTO_TCP := 6, IPPROTO_UDP := 17
  572. WS_Log(A_ThisFunc "(" protocol ", " ipversion ")", 3)
  573. sPtr := (A_PtrSize) ? A_PtrSize : 4
  574. Ptr := (A_PtrSize) ? "uptr" : "uint"
  575. af := (Instr(ipversion, "6")) ? AF_INET6 : AF_INET
  576. type := (Instr(protocol, "UDP")) ? SOCK_DGRAM : SOCK_STREAM
  577. protocol := (Instr(protocol, "udp")) ? IPPROTO_UDP : IPPROTO_TCP
  578. socket := DllCall("ws2_32\socket", "int", af, "int", type, "int", protocol, Ptr)
  579. if (socket=0)
  580. {
  581. WS_Log("[ERROR] Can't create this Socket!", 1)
  582. return 0
  583. }
  584. log := (protocol=IPPROTO_TCP) ? "TCP" : "UDP"
  585. log .= " socket created, for use with "
  586. log .= (af=AF_INET) ? "IPv4" : "IPv6"
  587. log .= " (Handle: " socket ")"
  588. WS_Log(log)
  589. return socket
  590. }
  591. WS_CloseSocket(byref socket)
  592. {
  593. WS_Log(A_ThisFunc "(" socket ")", 3)
  594. sPtr := (A_PtrSize) ? A_PtrSize : 4
  595. Ptr := (A_PtrSize) ? "uptr" : "uint"
  596. if (DllCall("ws2_32\closesocket", Ptr, socket, "int")!=0)
  597. {
  598. WS_Log("[ERROR] Can't close this Socket!", 1)
  599. return 0
  600. }
  601. WS_Log("Socket (" socket ") closed.")
  602. socket := 0
  603. return 1
  604. }
  605. WS_Startup(version = "2.0")
  606. {
  607. WS_Log(A_ThisFunc "(" version ")", 3)
  608. sPtr := (A_PtrSize) ? A_PtrSize : 4
  609. Ptr := (A_PtrSize) ? "uptr" : "uint"
  610. hWS := DllCall("LoadLibrary", "str", "ws2_32", Ptr)
  611. if (hWS=0)
  612. {
  613. WS_Log("[ERROR] Winsock Library can't be loaded!", 2)
  614. return 0
  615. }
  616. WS_Log("Winsock Library loaded. (Handle: " hWS ")")
  617. VarSetCapacity(WSAData, 394+sPtr, 0)
  618. if (instr(version, "."))
  619. {
  620. wVersionRequested := (Substr(version, 1, Instr(version, ".")-1) & 0xFF)
  621. wVersionRequested |= (Substr(version, Instr(version, ".")+1) & 0xFF) << 8
  622. }
  623. else
  624. wVersionRequested := version & 0xFF
  625. result := DllCall("ws2_32\WSAStartup", "ushort", wVersionRequested, Ptr, &WSAData, "int")
  626. if (result!=0)
  627. {
  628. WS_Log("[ERROR] Can't start up the Winsock Library! (errorcode: " result ")", 2)
  629. return 0
  630. }
  631. WS_Log("Winsock started.")
  632. WS_Log("Startup-Info, Winsock version: " NumGet(WSAData, 0, "uchar") "." NumGet(WSAData, 1, "uchar"))
  633. WS_Log("Startup-Info, Highest allowed Winsock version: " NumGet(WSAData, 2, "uchar") "." NumGet(WSAData, 3, "uchar"))
  634. Loop, 256
  635. szDescription .= Chr(NumGet(WSAData, A_Index+3, "uchar"))
  636. WS_Log("Startup-Info, Description: " szDescription)
  637. Loop, 128
  638. szSystemStatus .= Chr(NumGet(WSAData, A_Index+259, "uchar"))
  639. WS_Log("Startup-Info, System status: " szSystemStatus)
  640. return 1
  641. }
  642. WS_Shutdown()
  643. {
  644. WS_Log(A_ThisFunc "()", 3)
  645. Ptr := (A_PtrSize) ? "uptr" : "uint"
  646. result := DllCall("ws2_32\WSACleanup", "int")
  647. if (result!=0)
  648. WS_Log("[ERROR] Can't shut down the Winsock Library! (errorcode: " result ")", 2)
  649. else
  650. WS_Log("Winsock cleaned up.")
  651. hWS := DllCall("GetModuleHandle", "str", "ws2_32", Ptr)
  652. if (hWS=0)
  653. {
  654. WS_Log("[ERROR] Can't request the Module Handle for the Winsock Library!", 2)
  655. WS_Log("[ERROR] Maybe the Winsock Library is not loaded!", 2)
  656. }
  657. else
  658. {
  659. if (DllCall("FreeLibrary", Ptr, hWS)=0)
  660. WS_Log("[ERROR] The Winsock Library can't be unloaded!", 2)
  661. else
  662. WS_Log("Winsock Library unloaded. (Handle: " hWS ")")
  663. }
  664. return 1
  665. }
  666. WS_Log(str, type=0)
  667. {
  668. global WS_NOLOG, WS_LOGTOCONSOLE
  669. static log, cmd, stdout
  670. Ptr := (A_PtrSize) ? "uptr" : "uint"
  671. if (WS_NOLOG=1)
  672. return 0
  673. if (type=1)
  674. str .= " (errorcode: " DllCall("ws2_32\WSAGetLastError", "int") ")"
  675. if (WS_LOGTOCONSOLE)
  676. {
  677. if ((!cmd) || (!stdout))
  678. {
  679. cmd := DllCall("AllocConsole", "uint")
  680. stdout := DllCall("GetStdHandle", "uint", -11, Ptr)
  681. if ((!cmd) || (!stdout))
  682. WS_LOGTOCMD=0
  683. }
  684. if (stdout)
  685. {
  686. str .= "`n"
  687. time := A_Hour ":" A_Min ":" A_Sec " - "
  688. DllCall("WriteConsole", Ptr, stdout, Ptr, &time, "uint", strlen(time), Ptr "*", numwritten, Ptr, 0, "uint")
  689. if (type=1 || type=2)
  690. DllCall("SetConsoleTextAttribute", Ptr, stdout, "ushort", 0x0C)
  691. else if (type=3)
  692. DllCall("SetConsoleTextAttribute", Ptr, stdout, "ushort", 0x03)
  693. else if (type=4)
  694. DllCall("SetConsoleTextAttribute", Ptr, stdout, "ushort", 0x0A)
  695. else if (type=5)
  696. DllCall("SetConsoleTextAttribute", Ptr, stdout, "ushort", 0x0E)
  697. else
  698. DllCall("SetConsoleTextAttribute", Ptr, stdout, "ushort", 0x0F)
  699. DllCall("WriteConsole", Ptr, stdout, Ptr, &str, "uint", strlen(str), Ptr "*", numwritten, Ptr, 0, "uint")
  700. DllCall("SetConsoleTextAttribute", Ptr, stdout, "ushort", 0x0F)
  701. }
  702. }
  703. else
  704. {
  705. if (str!="")
  706. {
  707. log .= (log!="") ? "`n" : ""
  708. log .= A_DD "." A_MM "." A_YYYY " " A_Hour ":" A_Min ":" A_Sec
  709. log .= " - " str
  710. return 1
  711. }
  712. else
  713. {
  714. l := log
  715. log := ""
  716. return l
  717. }
  718. }
  719. return 1
  720. }
  721. WS_GetConsoleInput()
  722. {
  723. global WS_LOGTOCONSOLE
  724. if (!WS_LOGTOCONSOLE)
  725. return 0
  726. Ptr := (A_PtrSize) ? "uptr" : "uint"
  727. stdout := DllCall("GetStdHandle", "uint", -11, Ptr)
  728. stdin := DllCall("GetStdHandle", "uint", -10, Ptr)
  729. if ((stdin=0) || (stdout=0))
  730. return
  731. cmdlog := A_Hour ":" A_Min ":" A_Sec " - User Input: "
  732. DllCall("WriteConsole", Ptr, stdout, Ptr, &cmdlog, "uint", strlen(cmdlog), Ptr "*", numwritten, Ptr, 0, "uint")
  733. VarSetCapacity(buffer, 1024)
  734. if ((!DllCall("ReadConsole", Ptr, stdin, Ptr, &buffer, "uint", 1024, Ptr "*", numreaded, Ptr, 0, "uint")) || (numreaded=0))
  735. return
  736. Loop, % numreaded
  737. msg .= Chr(NumGet(buffer, (A_Index-1) * ((A_IsUnicode) ? 2 : 1), (A_IsUnicode) ? "ushort" : "uchar"))
  738. return msg
  739. }
  740. WS_GetLog()
  741. {
  742. return WS_Log("")
  743. }
  744. #singleinstance Force
  745. OnExit, ExitRoutine
  746. WS_LOGTOCONSOLE := 1
  747. global trm := chr(13) chr(10)
  748. initialize_WS()
  749. return
  750. initialize_WS(){
  751. WS_Startup()
  752. server := WS_Socket("TCP", "IPv4")
  753. WS_Bind(server, "0.0.0.0", "12345")
  754. WS_Listen(server)
  755. WS_HandleEvents(server, "ACCEPT READ CLOSE")
  756. }
  757. WS_OnAccept(skt){
  758. skt := WS_Accept(skt, client_ip, client_port)
  759. client[skt] := new client(skt)
  760. nck := client[skt].nick := "Guest" A_TickCount
  761. WS_Send(skt, "USRN||" nck trm)
  762. protJOIN(skt,"#a",nck)
  763. }
  764. WS_OnRead(skt){
  765. WS_Recv(skt, c),ClientMessage:=rtrim(c,"`n"),nck := client[skt].nick
  766. if (substr(str:=ClientMessage,2,1) == "W")
  767. {
  768. StringReplace, str,str, `r,,All
  769. getRegexArgs(str,arg1,arg2,arg3)
  770. protNWCD(skt,arg3,arg2)
  771. return
  772. }
  773. Loop, Parse, ClientMessage, `n
  774. {
  775. if !(ClientMessage := rtrim(A_LoopField,"`r`n"))
  776. return
  777. getRegexArgs(ClientMessage,arg1,arg2,arg3,arg4,arg5),protType := arg1
  778. if (protType == "MESG")
  779. protMESG(skt,arg2,nck,arg3)
  780. else if (protType == "JOIN")
  781. protJOIN(skt,arg2,nck)
  782. else if (protType == "NKCH")
  783. protNKCH(skt,arg2,nck)
  784. else if (protType == "RQCD")
  785. protRQCD(skt,arg2,arg3)
  786. }
  787. }
  788. protMESG(skt,chn,nck="",msg=""){
  789. for n,s in client.chanKeep[chn]
  790. WS_Send(s, "MESG||" chn "||" nck "||" msg trm)
  791. }
  792. protJOIN(skt,chn,nck){
  793. if (client[skt].chans[chn])
  794. {
  795. WS_Send(skt, "JOIN||" chn "||" "||||You are already in '" chn "'")
  796. return
  797. }
  798. client[skt].addChan(chn)
  799. for n,s in client.chanKeep[chn]
  800. WS_Send(s, "JOIN||" chn "||" nck "||" client.getNkList(chn) trm)
  801. }
  802. protNKCH(skt,nnk,onk){
  803. StringReplace,nnk,nnk,%a_space%,,All
  804. onk := client[skt].nick
  805. client[skt].nick := nnk
  806. for c in client[skt].chans
  807. for n in client.chanKeep[c]
  808. list.=n " "
  809. sort,list,UD%a_space%
  810. list:=trim(list," ")
  811. loop,parse,list,%a_space%
  812. WS_Send(client.getSock(a_loopfield), "NKCH||" onk "||" nnk trm)
  813. client.removeNick(skt,onk,nnk)
  814. }
  815. protNWCD(skt,cod,ver){
  816. nck := client[skt].nick
  817. client[skt].addCode(cod,ver)
  818. for c in client.chanKeep
  819. for n, s in client.chanKeep[c]
  820. list.=s " "
  821. sort,list,UD%A_Space%
  822. list:=trim(list," ")
  823. loop, parse, list, %A_Space%
  824. if A_LoopField
  825. WS_Send(A_LoopField, "NWCD||" ver "||" nck "||Notice: """ nck """ submitted new code." trm)
  826. }
  827. protRQCD(skt,nck,ver){
  828. WS_Send(skt, "RQCD||" nck "||" ver "||" client[client.getsock(nck)].codes[ver] trm)
  829. }
  830. WS_OnClose(skt){
  831. for c in client.chanKeep
  832. for n,s in client.chanKeep[c]
  833. {
  834. if (s == skt)
  835. client.chanKeep[c].Remove(n)
  836. if (s != skt)
  837. list.=s " "
  838. }
  839. sort,list,UD%A_Space%
  840. list:=trim(list," ")
  841. loop,parse,list, %a_space%
  842. WS_Send(a_loopfield,"DISC||" client[skt].nick)
  843. client.sockNick.Remove(client[skt].nick)
  844. client[skt]:=""
  845. WS_CloseSocket(skt)
  846. }
  847. getRegexArgs(str, byref a1,byref a2,byref a3 = "",byref a4 = "",byref a5 = ""){
  848. RegexReplace(str, "\|\|", "", cnt)
  849. loop, %cnt%
  850. search.="(.*)\|\|"
  851. search .= "(.*)"
  852. RegexMatch(str, "^" search, a)
  853. }
  854. m(x*){
  855. for a,b in x
  856. list.=b "`n"
  857. MsgBox,% list
  858. }
  859. t(x*){
  860. for a,b in x
  861. list.=b "`n"
  862. ToolTip,% list
  863. }
  864. d(x*){
  865. for a,b in x
  866. list.=b "|"
  867. OutputDebug,%list%
  868. }
  869. class client {
  870. static chanKeep:=[],sockNick:=[]
  871. __New(val){
  872. this.sock:=val
  873. }
  874. __Set(key,val){
  875. if (key == "nick")
  876. client.setSockNick(val,this.sock)
  877. }
  878. addCode(cod,ver){
  879. static codes:=[]
  880. this.codes[ver]:=cod
  881. }
  882. setSockNick(nck,skt){
  883. client.sockNick[nck]:=skt
  884. }
  885. getSock(nck){
  886. return client.sockNick[nck]
  887. }
  888. removeNick(skt,onk,nnk){
  889. client.sockNick.Remove(onk)
  890. client.setSockNick(nnk,skt)
  891. for c in client[skt].chans
  892. {
  893. client.chanKeep[c].Remove(onk)
  894. client.chanKeep[c,nnk] := skt
  895. }
  896. }
  897. addChan(chn){
  898. static chans:=[]
  899. this.chans[chn] := 1
  900. client.chanKeep[chn,this.nick] := this.sock
  901. }
  902. getNkList(chn){
  903. for n in client.chanKeep[chn]
  904. list .= n " "
  905. return trim(list," ")
  906. }
  907. }
  908. ExitRoutine:
  909. WS_CloseSocket(server)
  910. WS_Shutdown()
  911. ExitApp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement