Skip to content

Commit

Permalink
add: i18n support
Browse files Browse the repository at this point in the history
  • Loading branch information
XZB-1248 committed Apr 12, 2022
1 parent e8053cb commit e2941e0
Show file tree
Hide file tree
Showing 31 changed files with 661 additions and 351 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## v0.0.4

* Add: i18n support

* 新增:支持中英文国际化



## v0.0.3

* Add: network IO speed monitoring.
Expand Down
5 changes: 1 addition & 4 deletions README.ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

现在暂时只支持本地安装,暂时还没有Docker等安装方式。

<details>
<summary>本地安装</summary>
### 本地安装

*[Releases](https://github.com/XZB-1248/Spark/releases) 页面下载对应系统的可执行文件。
* 修改配置文件,特别是salt,需要修改成你自己的。
Expand All @@ -36,8 +35,6 @@
* 在管理页面中生成客户端,并部署到设备上。
* 现在就可以控制这个设备了。

</details>

---

## **特性**
Expand Down
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ that allow you to control all your devices via browser anywhere.

Only local installation are available yet.

<details>
<summary>Local installation:</summary>

### Local installation
* Get prebuilt executable file from [Releases](https://github.com/XZB-1248/Spark/releases) page.
* Modify configuration file and set your own salt.

Expand All @@ -27,12 +25,10 @@ Only local installation are available yet.
}
```

* Run it and browser the address:port you've just set.
* Run it and browse the address:port you've just set.
* Generate client online and execute it on your device.
* Now you can control your device.

</details>

---

## **Features**
Expand Down
2 changes: 1 addition & 1 deletion client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Cfg struct {
// none
var CfgBuffer = "\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19"

// COMMIT means this commit hash, for auto upgrade.
// COMMIT means this commit hash, help to identify version and self upgrade.
var COMMIT = ``
var Config Cfg

Expand Down
24 changes: 12 additions & 12 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import (
"Spark/utils"
"encoding/hex"
"errors"
ws "github.com/gorilla/websocket"
"github.com/imroc/req/v3"
"github.com/kataras/golog"
"io/ioutil"
"net/http"
"os"
"os/exec"
"runtime"
"strconv"
"time"

ws "github.com/gorilla/websocket"
"github.com/imroc/req/v3"
"github.com/kataras/golog"
)

// simplified type of map
Expand Down Expand Up @@ -78,6 +79,9 @@ func connectWS() (*common.Conn, error) {
return nil, errNoSecretHeader
}
secret, err := hex.DecodeString(header[0])
if err != nil {
return nil, err
}
return &common.Conn{Conn: wsConn, Secret: secret}, nil
}

Expand Down Expand Up @@ -246,13 +250,10 @@ func handleAct(pack modules.Packet, wsConn *common.Conn) {
if err != nil {
common.SendCb(modules.Packet{Act: `initTerminal`, Code: 1, Msg: err.Error()}, pack, wsConn)
}
break
case `inputTerminal`:
terminal.InputTerminal(pack)
break
case `killTerminal`:
terminal.KillTerminal(pack)
break
case `listFiles`:
path := `/`
if val, ok := pack.Data[`path`]; ok {
Expand Down Expand Up @@ -293,11 +294,11 @@ func handleAct(pack modules.Packet, wsConn *common.Conn) {
{
tempVal, ok := pack.Data[`file`]
if !ok {
common.SendCb(modules.Packet{Code: 1, Msg: `未知错误`}, pack, wsConn)
common.SendCb(modules.Packet{Code: 1, Msg: `${i18n|unknownError}`}, pack, wsConn)
return
}
if path, ok = tempVal.(string); !ok {
common.SendCb(modules.Packet{Code: 1, Msg: `未知错误`}, pack, wsConn)
common.SendCb(modules.Packet{Code: 1, Msg: `${i18n|unknownError}`}, pack, wsConn)
return
}
tempVal, ok = pack.Data[`start`]
Expand All @@ -316,7 +317,7 @@ func handleAct(pack modules.Packet, wsConn *common.Conn) {
}
}
if end > 0 && end < start {
common.SendCb(modules.Packet{Code: 1, Msg: `文件范围错误`}, pack, wsConn)
common.SendCb(modules.Packet{Code: 1, Msg: `${i18n|invalidFileRange}`}, pack, wsConn)
return
}
}
Expand All @@ -334,12 +335,12 @@ func handleAct(pack modules.Packet, wsConn *common.Conn) {
case `killProcess`:
pidStr, ok := pack.Data[`pid`]
if !ok {
common.SendCb(modules.Packet{Code: 1, Msg: `未知错误`}, pack, wsConn)
common.SendCb(modules.Packet{Code: 1, Msg: `${i18n|unknownError}`}, pack, wsConn)
return
}
pid, err := strconv.ParseInt(pidStr.(string), 10, 32)
if err != nil {
common.SendCb(modules.Packet{Code: 1, Msg: `未知错误`}, pack, wsConn)
common.SendCb(modules.Packet{Code: 1, Msg: `${i18n|unknownError}`}, pack, wsConn)
return
}
err = process.KillProcess(int32(pid))
Expand All @@ -351,7 +352,6 @@ func handleAct(pack modules.Packet, wsConn *common.Conn) {
default:
common.SendCb(modules.Packet{Code: 0}, pack, wsConn)
}
return
}

func heartbeat(wsConn *common.Conn) error {
Expand Down
5 changes: 3 additions & 2 deletions client/service/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package file
import (
"Spark/client/config"
"errors"
"github.com/imroc/req/v3"
"io"
"io/ioutil"
"os"
"strconv"

"github.com/imroc/req/v3"
)

type file struct {
Expand Down Expand Up @@ -58,7 +59,7 @@ func UploadFile(path, trigger string, start, end int64) error {
`FileSize`: strconv.FormatInt(size, 10),
}
if size < end {
return errors.New(`文件大小有误`)
return errors.New(`Invalid file size.`)
}
if end == 0 {
uploadReq.RawRequest.ContentLength = size - start
Expand Down
13 changes: 7 additions & 6 deletions client/service/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"bytes"
"encoding/hex"
"errors"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io"
"io/ioutil"
"os"
"os/exec"
"runtime"
"time"

"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)

type terminal struct {
Expand Down Expand Up @@ -124,12 +125,12 @@ func InputTerminal(pack modules.Packet) error {
}
val, ok = terminals.Get(termUUID)
if !ok {
common.SendCb(modules.Packet{Act: `quitTerminal`, Msg: `终端已退出`}, pack, common.WSConn)
common.SendCb(modules.Packet{Act: `quitTerminal`, Msg: `${i18n|terminalSessionClosed}`}, pack, common.WSConn)
return nil
}
terminal, ok := val.(*terminal)
if !ok {
common.SendCb(modules.Packet{Act: `quitTerminal`, Msg: `终端已退出`}, pack, common.WSConn)
common.SendCb(modules.Packet{Act: `quitTerminal`, Msg: `${i18n|terminalSessionClosed}`}, pack, common.WSConn)
return nil
}

Expand Down Expand Up @@ -157,13 +158,13 @@ func KillTerminal(pack modules.Packet) error {
}
val, ok = terminals.Get(termUUID)
if !ok {
common.SendCb(modules.Packet{Act: `quitTerminal`, Msg: `终端已退出`}, pack, common.WSConn)
common.SendCb(modules.Packet{Act: `quitTerminal`, Msg: `${i18n|terminalSessionClosed}`}, pack, common.WSConn)
return nil
}
terminal, ok := val.(*terminal)
if !ok {
terminals.Remove(termUUID)
common.SendCb(modules.Packet{Act: `quitTerminal`, Msg: `终端已退出`}, pack, common.WSConn)
common.SendCb(modules.Packet{Act: `quitTerminal`, Msg: `${i18n|terminalSessionClosed}`}, pack, common.WSConn)
return nil
}
doKillTerminal(terminal)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ require (
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/net v0.0.0-20220111093109-d55c255bac03 // indirect
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/text v0.3.7
gopkg.in/yaml.v2 v2.2.8 // indirect
)
15 changes: 8 additions & 7 deletions server/common/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type EventCallback func(modules.Packet, *melody.Session)

var eventTable = cmap.New()

// CallEvent 负责判断packet中的Callback字段,如果存在该字段,
// 就会调用event中的函数,并在调用完成之后通过chan通知addOnceEvent调用方
// CallEvent tries to call the callback with the given uuid
// after that, it will notify the caller via the channel
func CallEvent(pack modules.Packet, session *melody.Session) {
if len(pack.Event) == 0 {
return
Expand All @@ -40,8 +40,9 @@ func CallEvent(pack modules.Packet, session *melody.Session) {
}
}

// AddEventOnce 会添加一个一次性的回调命令,client可以对事件成功与否进行回复
// trigger一般是uuid,以此尽可能保证事件的独一无二
// AddEventOnce adds a new event only once and client
// can call back the event with the given event trigger.
// Event trigger should be uuid to make every event unique.
func AddEventOnce(fn EventCallback, connUUID, trigger string, timeout time.Duration) bool {
done := make(chan bool)
ev := &event{
Expand All @@ -59,8 +60,8 @@ func AddEventOnce(fn EventCallback, connUUID, trigger string, timeout time.Durat
}
}

// AddEvent 会添加一个持续的回调命令,client可以对事件成功与否进行回复
// trigger一般是uuid,以此尽可能保证事件的独一无二
// AddEvent adds a new event and client can call back
// the event with the given event trigger.
func AddEvent(fn EventCallback, connUUID, trigger string) {
ev := &event{
connection: connUUID,
Expand All @@ -70,7 +71,7 @@ func AddEvent(fn EventCallback, connUUID, trigger string) {
eventTable.Set(trigger, ev)
}

// RemoveEvent 会删除特定的回调命令
// RemoveEvent deletes the event with the given event trigger.
func RemoveEvent(trigger string) {
eventTable.Remove(trigger)
}
Expand Down
Loading

0 comments on commit e2941e0

Please sign in to comment.