TelwsView.vue 13 KB

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