dSockets.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // import {getDumpIP} from "./tun/tailscale_tun_auto.js"
  2. export class DirectNetwork {
  3. constructor() {}
  4. tcpSocket(ip, port, options) {
  5. return new TCPSocketClient(ip, port, options);
  6. }
  7. // directServerSocket(localAddress, options) {
  8. // return new DirectTCPServer(localAddress, options);
  9. // }
  10. }
  11. function uint32ToIp(uint32) {
  12. // backwards from what I thought?
  13. const a = uint32 & 0xFF; // First byte
  14. const b = (uint32 >>> 8) & 0xFF; // Second byte
  15. const c = (uint32 >>> 16) & 0xFF; // Third byte
  16. const d = (uint32 >>> 24) & 0xFF; // Fourth byte
  17. return `${a}.${b}.${c}.${d}`;
  18. }
  19. export class TCPSocketClient {
  20. constructor(ip, port, options) {
  21. this.localPort = null;
  22. this.socket = null;
  23. this.remoteAddress = null;
  24. this.remotePort = null;
  25. this.options = null;
  26. this.readable = null;
  27. this.writable = null;
  28. let ip_string = uint32ToIp(ip);
  29. this.socket = new TCPSocket(ip_string, port, options);
  30. console.log(`Created new TCPSocket: ip=[${ip_string}], port=[${port}], options=[${options}]`);
  31. }
  32. async connect() {
  33. try {
  34. const {readable, writable} = await this.socket.opened;
  35. if (readable && writable) {
  36. this.readable = readable;
  37. this.writable = writable;
  38. console.log("writable, readable: ", writable, readable);
  39. console.log("return 0");
  40. return 0;
  41. }
  42. console.log("return 1");
  43. return 1;
  44. } catch (e) {
  45. console.error(`TCPSocketClient failed to connect: `, e);
  46. return 2
  47. }
  48. }
  49. async send(data) {
  50. try {
  51. console.log("WEBVM: SEND");
  52. const writer = this.writable.getWriter();
  53. await writer.write(data);
  54. writer.releaseLock();
  55. return 0
  56. } catch (e) {
  57. console.log("Error sending: ", e);
  58. return 1;
  59. }
  60. }
  61. async recv() {
  62. try {
  63. console.log("WEBVM: RECV");
  64. const reader = this.readable.getReader();
  65. while (true) {
  66. const {value, done} = await reader.read();
  67. if (done) {
  68. break;
  69. }
  70. console.log("value in recv(): ", value);
  71. reader.releaseLock();
  72. return value;
  73. }
  74. reader.releaseLock();
  75. } catch (e) {
  76. console.log("Error Receiving: ", e);
  77. return null;
  78. }
  79. return null;
  80. }
  81. async close() {
  82. try {
  83. console.log("WEBVM: CLOSE");
  84. await this.socket.close();
  85. console.log("WEBVM: CLOSED");
  86. return 0
  87. } catch (e) {
  88. console.log("Error closing: ", e);
  89. return 1;
  90. }
  91. }
  92. }
  93. export async function autoConfSockets() {
  94. console.log(`AutoConfSockets running`);
  95. return {tcpSocket: TCPSocketClient}
  96. }
  97. export class DirectTCPServer {
  98. constructor(localAddress, options) {
  99. this.server = null;
  100. this.localAddress = localAddress;
  101. this.readable = null;
  102. let str =uint32ToIp(localAddress);
  103. this.server = new TCPServerSocket(localAddress, options);
  104. console.log(`Created new ServerSocket: localAddress = [${localAddress}], options = [${options}]`);
  105. }
  106. async bind() {
  107. this.readable = await this.server.opened;
  108. const reader = this.readable.getReader();
  109. while (true) {
  110. const {value: acceptedSocket, done} = await reader.read();
  111. if (done) {
  112. break;
  113. }
  114. console.log(`New Socket connected to server: `, value);
  115. }
  116. reader.releaseLock();
  117. }
  118. close() {
  119. this.server.close();
  120. }
  121. }