TelwsView.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <template>
  2. <v-layout
  3. column
  4. fill-height
  5. >
  6. <v-toolbar color="grey">
  7. <v-toolbar-title flex>
  8. {{ title }}
  9. </v-toolbar-title>
  10. <v-spacer />
  11. <v-text-field
  12. v-if="this.hideUrl == undefined"
  13. label="Telws URL"
  14. single-line
  15. prepend-icon="mdi-link-variant"
  16. hide-details
  17. v-model="telwsurl"
  18. />
  19. <v-btn
  20. icon
  21. @click="connect"
  22. >
  23. <v-icon>mdi-lan-connect</v-icon>
  24. </v-btn>
  25. <v-btn
  26. icon
  27. @click="disconnect"
  28. >
  29. <v-icon>mdi-lan-disconnect</v-icon>
  30. </v-btn>
  31. </v-toolbar>
  32. <v-layout fill-height>
  33. <xterm-vue
  34. ref="term"
  35. v-on:title-change="titleChange"
  36. />
  37. </v-layout>
  38. <v-toolbar color="grey">
  39. <v-text-field
  40. placeholder="Write here and press Enter to send..."
  41. single-line
  42. prepend-icon="mdi-typewriter"
  43. hide-details
  44. v-model="writeBarText"
  45. @keydown.enter="pasteWriteBar"
  46. />
  47. </v-toolbar>
  48. <v-dialog
  49. v-model="userAuthDialog"
  50. max-width="290"
  51. >
  52. <v-card>
  53. <v-card-title class="headline">Simple AAA Auth</v-card-title>
  54. <v-card-text>
  55. <v-form>
  56. <v-text-field
  57. ref="authUsername"
  58. v-model="authDialogUsername"
  59. label="Username"
  60. @keydown.enter="focusPasswordTextfield"
  61. />
  62. <v-text-field
  63. ref="authPassword"
  64. v-model="authDialogPassword"
  65. label="Password"
  66. type="password"
  67. @keydown.enter="doUserLogin"
  68. />
  69. </v-form>
  70. </v-card-text>
  71. <v-card-actions>
  72. <v-spacer></v-spacer>
  73. <v-btn
  74. color="danger"
  75. text
  76. @click="cancelUserLogin"
  77. >
  78. Cancel
  79. </v-btn>
  80. <v-btn
  81. color="success"
  82. text
  83. @click="doUserLogin"
  84. >
  85. Login
  86. </v-btn>
  87. </v-card-actions>
  88. </v-card>
  89. </v-dialog>
  90. </v-layout>
  91. </template>
  92. <script>
  93. //import XtermVuePlugin from "@swzry/xterm-vue";
  94. import axios from "axios";
  95. import smcrypto from "sm-crypto";
  96. const ConnState = {
  97. IDLE: 0,
  98. GET_INFO: 1,
  99. USER_AUTH: 2,
  100. SERVER_AUTH: 3,
  101. AUTH_OK: 4,
  102. CONNECTED: 5
  103. };
  104. export default {
  105. name: "telws_view",
  106. props: ["hideUrl", "defaultUrl", "autoConnect"],
  107. data: () => ({
  108. writeBarText: "",
  109. title: "Untitled",
  110. telwsurl: "",
  111. userAuthDialog: false,
  112. authDialogUsername: "",
  113. authDialogPassword: "",
  114. connSM: {
  115. state: ConnState.IDLE,
  116. axiosCancel: undefined,
  117. continueDo: false,
  118. url_prefix: "",
  119. auth_jwt: "",
  120. wsurl: "",
  121. wsconn: undefined
  122. },
  123. encryptConfig: undefined
  124. }),
  125. mounted() {
  126. this.$refs.term.fit();
  127. if (this.defaultUrl) {
  128. this.telwsurl = this.defaultUrl;
  129. }
  130. if (this.autoConnect != undefined) {
  131. this.connect();
  132. }
  133. },
  134. destroyed() {
  135. this.disconnect();
  136. },
  137. methods: {
  138. focusUsernameTextfield(){
  139. this.$refs.authUsername.focus()
  140. },
  141. focusPasswordTextfield(){
  142. this.$refs.authPassword.focus()
  143. },
  144. pasteWriteBar(event) {
  145. this.$refs.term.paste(this.writeBarText);
  146. this.writeBarText = "";
  147. },
  148. titleChange(title) {
  149. this.title = title;
  150. },
  151. popUserLoginDialog() {
  152. this.authDialogUsername = "";
  153. this.authDialogPassword = "";
  154. this.userAuthDialog = true;
  155. var self = this
  156. this.$nextTick(() => {
  157. self.focusUsernameTextfield();
  158. })
  159. },
  160. doUserLogin() {
  161. this.userAuthDialog = false;
  162. var authData = {
  163. method: "simple-aaa",
  164. username: this.authDialogUsername,
  165. password: this.authDialogPassword
  166. };
  167. var jsondata = JSON.stringify(authData);
  168. var cm = 1;
  169. if (this.encryptConfig.cipherMode === "C1C3C2") {
  170. cm = 1;
  171. } else {
  172. cm = 0;
  173. }
  174. if (this.encryptConfig) {
  175. var ct = smcrypto.sm2.doEncrypt(
  176. jsondata,
  177. this.encryptConfig.pubkey,
  178. cm
  179. );
  180. var tureCipher = this.encryptConfig.header + ct;
  181. this.doLoginAuth(tureCipher);
  182. } else {
  183. this.connSM.state = ConnState.IDLE;
  184. this.$refs.term.$term.writeln("Password encypt failed.");
  185. this.$refs.term.$term.onData(function(data) {});
  186. }
  187. },
  188. doWebsocketConnect() {
  189. if (this.connSM.wsconn) {
  190. this.connSM.wsconn.close();
  191. }
  192. this.$refs.term.$term.writeln(
  193. "Connecting Websocket '" + this.connSM.wsurl + "'..."
  194. );
  195. var trueUrl = this.connSM.wsurl + "?token=" + this.connSM.auth_jwt
  196. this.connSM.wsconn = new WebSocket(trueUrl);
  197. var tty = this.$refs.term.$term;
  198. var wsconn = this.connSM.wsconn;
  199. var self = this
  200. wsconn.onopen = function() {
  201. //wsconn.binaryType = "arrayBuffer"
  202. wsconn.binaryType = "blob"
  203. tty.writeln("Connected.\r\n");
  204. self.connSM.state = ConnState.CONNECTED
  205. };
  206. wsconn.onmessage = function(event) {
  207. var er = new FileReader();
  208. er.onload = function(e) {
  209. var buf = new Uint8Array(er.result);
  210. tty.write(buf);
  211. };
  212. er.readAsArrayBuffer(event.data);
  213. };
  214. wsconn.onclose = function() {
  215. tty.writeln("\r\n\r\nConnection Closed.");
  216. };
  217. wsconn.onerror = function() {
  218. tty.writeln("\r\n\r\nConnection Error.");
  219. };
  220. this.$refs.term.$term.onData(function(data) {
  221. if (wsconn) {
  222. var bd = Buffer.from(data, "utf8");
  223. wsconn.send(bd);
  224. }
  225. });
  226. },
  227. doLoginAuth(authData) {
  228. var adjson = { authData: authData };
  229. var self = this;
  230. this.connSM.state = ConnState.SERVER_AUTH;
  231. axios
  232. .post(this.connSM.url_prefix + "login.satori", adjson, {
  233. cancelToken: new axios.CancelToken(function executor(c) {
  234. self.connSM.cancelToken = c;
  235. })
  236. })
  237. .then(resp => {
  238. if (resp.data.suc) {
  239. this.connSM.auth_jwt = resp.data.jwt;
  240. this.connSM.state = ConnState.AUTH_OK;
  241. this.doWebsocketConnect();
  242. } else {
  243. if (resp.data.etype == "auth_failed") {
  244. this.$refs.term.$term.writeln("Simple AAA Auth Failed.");
  245. this.$refs.term.$term.writeln(
  246. "Reason: " + resp.data.auth_error_msg
  247. );
  248. } else {
  249. this.$refs.term.$term.writeln("Failed to do remote auth.");
  250. this.$refs.term.$term.writeln("Error: " + resp.data.etype);
  251. }
  252. this.connSM.state = ConnState.IDLE;
  253. }
  254. })
  255. .catch(errmsg => {
  256. this.$refs.term.$term.writeln("Failed to do remote auth.");
  257. console.warn(errmsg);
  258. if (errmsg.response) {
  259. console.warn(errmsg.response);
  260. this.$refs.term.$term.writeln("Error: " + errmsg.response.data);
  261. } else if (errmsg.request) {
  262. console.warn(errmsg.request);
  263. this.$refs.term.$term.writeln(
  264. "Error: request failed. See javascript console for detail."
  265. );
  266. } else {
  267. this.$refs.term.$term.writeln(
  268. "Error: unknown. See javascript console for more detail."
  269. );
  270. }
  271. this.connSM.state = ConnState.IDLE;
  272. });
  273. },
  274. cancelUserLogin() {
  275. this.userAuthDialog = false;
  276. this.connSM.state = ConnState.IDLE;
  277. this.$refs.term.$term.writeln("Auth cancel by user.");
  278. this.$refs.term.$term.onData(function(data) {});
  279. },
  280. disconnect() {
  281. if (this.connSM.state == ConnState.IDLE) {
  282. return;
  283. }
  284. this.connSM.continueDo = false;
  285. if (this.connSM.cancelToken) {
  286. this.connSM.cancelToken();
  287. }
  288. this.connSM.state = ConnState.IDLE;
  289. this.$refs.term.$term.writeln("");
  290. this.$refs.term.$term.writeln("Disconnected.");
  291. this.$refs.term.$term.onData(function(data) {});
  292. if(this.connSM.wsconn){
  293. this.connSM.wsconn.close()
  294. }
  295. },
  296. connect() {
  297. if (this.connSM.state != ConnState.IDLE) {
  298. this.disconnect();
  299. }
  300. this.$refs.term.$term.clear();
  301. this.connSM.url_prefix = this.telwsurl + "/";
  302. var turl = new URL(this.connSM.url_prefix);
  303. var schpre = "ws:";
  304. if (turl.protocol === "https:") {
  305. schpre = "wss:";
  306. }
  307. this.connSM.wsurl = schpre + "//" + turl.host + turl.pathname + "ws.satori"
  308. this.$refs.term.$term.writeln("Connecting " + this.telwsurl + "...");
  309. var self = this;
  310. this.connSM.continueDo = true;
  311. this.connSM.state = ConnState.GET_INFO;
  312. axios
  313. .get(this.telwsurl, {
  314. cancelToken: new axios.CancelToken(function executor(c) {
  315. self.connSM.cancelToken = c;
  316. })
  317. })
  318. .then(resp => {
  319. if (self.connSM.continueDo) {
  320. this.$refs.term.$term.writeln("Telws server info fetched.");
  321. console.log(resp.data);
  322. if (resp.data.auth_methods) {
  323. if (resp.data.auth_methods.indexOf("simple-aaa") > -1) {
  324. this.encryptConfig = resp.data.encrypt;
  325. this.connSM.state = ConnState.USER_AUTH;
  326. this.popUserLoginDialog();
  327. } else {
  328. this.$refs.term.$term.writeln(
  329. "Failed to connect to telws server."
  330. );
  331. this.$refs.term.$term.writeln(
  332. "Error: no auth method supported by this client."
  333. );
  334. this.connSM.state = ConnState.IDLE;
  335. }
  336. } else {
  337. this.$refs.term.$term.writeln(
  338. "Failed to connect to telws server."
  339. );
  340. this.$refs.term.$term.writeln("Error: not a valid telws server.");
  341. this.connSM.state = ConnState.IDLE;
  342. }
  343. }
  344. })
  345. .catch(errmsg => {
  346. this.$refs.term.$term.writeln("Failed to connect to telws server.");
  347. console.warn(errmsg);
  348. if (errmsg.response) {
  349. console.warn(errmsg.response);
  350. this.$refs.term.$term.writeln("Error: " + errmsg.response.data);
  351. } else if (errmsg.request) {
  352. console.warn(errmsg.request);
  353. this.$refs.term.$term.writeln(
  354. "Error: request failed. See javascript console for detail."
  355. );
  356. } else {
  357. this.$refs.term.$term.writeln(
  358. "Error: unknown. See javascript console for more detail."
  359. );
  360. }
  361. this.connSM.state = ConnState.IDLE;
  362. });
  363. }
  364. }
  365. };
  366. </script>