[GH-ISSUE #2300] [BUG] 命令行参数解析不正确 #1456

Closed
opened 2026-02-27 00:03:37 +03:00 by kerem · 0 comments
Owner

Originally created by @MicroOps-cn on GitHub (Nov 5, 2021).
Original GitHub issue: https://github.com/electerm/electerm/issues/2300

Please check known issues first

https://github.com/electerm/electerm/wiki/Know-issues

Electerm version:

1.17.3

Operating system(linux, macos, or windows7/8/10?):

linux

Current Behavior

在执行electerm root@xxx.com:2020的时候,应该会连接xxx.com的2020端口,但是实际上连接的仍是xxx.com的22端口,

源代码

function getHost (argv, opts) {
  const arr = argv
  let i = arr.length - 1
  const reg = /^([\w\d-_]+@)?([\w\d-_]+\.[\w\d-_.]+)(:[\d]+)?$/
  for (; i >= 0; i--) {
    const str = arr[i]
    const mt = str.match(reg)
    if (mt) {
      const port = mt[3]
      const user = mt[1]
      return {
        host: mt[2],
        username: user ? user.replace('@', '') : user,
        port: port ? parseInt(port, 10) : 22
      }
    }
  }
  return {}
}

原代码中的正则表达式匹配结果是port=":2020",然后parseInt(port, 10),返回的结果肯定是NaN,也就是最终解析的端口始终会是22

Possible Solution

原代码中的正则表达式可以优化一下

function getHost (argv, opts) {
  const arr = argv
  let i = arr.length - 1
  const reg = /^(?:([\w\d-_]+)@)?([\w\d-_]+\.[\w\d-_.]+)(?::([\d]+))?$/
  for (; i >= 0; i--) {
    const str = arr[i]
    const mt = str.match(reg)
    if (mt) {
      const port = mt[3]
      const user = mt[1]
      return {
        host: mt[2],
        username: user,
        port: port ? parseInt(port, 10) : 22
      }
    }
  }
  return {}
}

测试正则表达式

> getHost(["electerm","root@xxx.com:2020"],{})
{ host: 'xxx.com', username: 'root', port: 2020 }
> getHost(["electerm","xxx.com:2020"],{})
{ host: 'xxx.com', username: undefined, port: 2020 }
> getHost(["electerm","xxx.com"],{})
{ host: 'xxx.com', username: undefined, port: 22 }
> getHost(["electerm","root@xxx.com"],{})
{ host: 'xxx.com', username: 'root', port: 22 }
Originally created by @MicroOps-cn on GitHub (Nov 5, 2021). Original GitHub issue: https://github.com/electerm/electerm/issues/2300 ## Please check known issues first https://github.com/electerm/electerm/wiki/Know-issues <!--- Provide some basic info --> ## Electerm version: 1.17.3 ## Operating system(linux, macos, or windows7/8/10?): linux ## Current Behavior 在执行`electerm root@xxx.com:2020`的时候,应该会连接xxx.com的2020端口,但是实际上连接的仍是xxx.com的22端口, [源代码](https://github.com/electerm/electerm/blob/master/src/client/store/load-data.js#L13-L31) ```js function getHost (argv, opts) { const arr = argv let i = arr.length - 1 const reg = /^([\w\d-_]+@)?([\w\d-_]+\.[\w\d-_.]+)(:[\d]+)?$/ for (; i >= 0; i--) { const str = arr[i] const mt = str.match(reg) if (mt) { const port = mt[3] const user = mt[1] return { host: mt[2], username: user ? user.replace('@', '') : user, port: port ? parseInt(port, 10) : 22 } } } return {} } ``` 原代码中的正则表达式匹配结果是port=":2020",然后parseInt(port, 10),返回的结果肯定是NaN,也就是最终解析的端口始终会是22 ## Possible Solution 原代码中的正则表达式可以优化一下 ```js function getHost (argv, opts) { const arr = argv let i = arr.length - 1 const reg = /^(?:([\w\d-_]+)@)?([\w\d-_]+\.[\w\d-_.]+)(?::([\d]+))?$/ for (; i >= 0; i--) { const str = arr[i] const mt = str.match(reg) if (mt) { const port = mt[3] const user = mt[1] return { host: mt[2], username: user, port: port ? parseInt(port, 10) : 22 } } } return {} } ``` 测试正则表达式 ``` > getHost(["electerm","root@xxx.com:2020"],{}) { host: 'xxx.com', username: 'root', port: 2020 } > getHost(["electerm","xxx.com:2020"],{}) { host: 'xxx.com', username: undefined, port: 2020 } > getHost(["electerm","xxx.com"],{}) { host: 'xxx.com', username: undefined, port: 22 } > getHost(["electerm","root@xxx.com"],{}) { host: 'xxx.com', username: 'root', port: 22 } ```
kerem closed this issue 2026-02-27 00:03:37 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/electerm#1456
No description provided.