Skip to content

Commit

Permalink
添加菜单
Browse files Browse the repository at this point in the history
  • Loading branch information
yhy0 committed May 23, 2023
1 parent 785a56b commit 407cc05
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 22 deletions.
96 changes: 96 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/buger/jsonparser"
uuid "github.com/satori/go.uuid"
"github.com/wailsapp/wails/v2/pkg/menu"
"github.com/wailsapp/wails/v2/pkg/menu/keys"
"github.com/wailsapp/wails/v2/pkg/runtime"
"github.com/yhy0/ChYing/conf"
"github.com/yhy0/ChYing/pkg/file"
"github.com/yhy0/ChYing/pkg/httpx"
"github.com/yhy0/ChYing/pkg/log"
"github.com/yhy0/ChYing/pkg/utils"
Expand All @@ -31,6 +35,98 @@ func NewApp() *App {
return &App{}
}

// Menu 应用菜单
func (a *App) Menu() *menu.Menu {
return menu.NewMenuFromItems(
menu.SubMenu("承影", menu.NewMenuFromItems(
menu.Text("关于", nil, func(_ *menu.CallbackData) {
a.diag(conf.Description, false)
}),
menu.Text("检查更新", nil, func(_ *menu.CallbackData) {
resp, err := httpx.Get("https://api.github.com/repos/yhy0/ChYing/tags")
if err != nil {
a.diag("检查更新出错\n"+err.Error(), true)
return
}

lastVersion, err := jsonparser.GetString([]byte(resp.Body), "[0]", "name")
if err != nil {
a.diag("检查更新出错\n"+err.Error(), true)
return
}

needUpdate := conf.Version < lastVersion
msg := conf.VersionNewMsg
btns := []string{conf.BtnConfirmText}
if needUpdate {
msg = fmt.Sprintf(conf.VersionOldMsg, lastVersion)
btns = []string{"确定", "取消"}
}
selection, err := a.diag(msg, false, btns...)
if err != nil {
return
}
if needUpdate && selection == conf.BtnConfirmText {
url := fmt.Sprintf("https://github.com/yhy0/ChYing/releases/tag/%s", lastVersion)
runtime.BrowserOpenURL(a.ctx, url)
}
}),
menu.Text(
"主页",
keys.Combo("H", keys.CmdOrCtrlKey, keys.ShiftKey),
func(_ *menu.CallbackData) {
runtime.BrowserOpenURL(a.ctx, "https://github.com/yhy0/ChYing/")
},
),
menu.Separator(),
menu.Text("退出", keys.CmdOrCtrl("Q"), func(_ *menu.CallbackData) {
runtime.Quit(a.ctx)
}),
)),

menu.EditMenu(),
menu.SubMenu("Help", menu.NewMenuFromItems(
menu.Text(
"打开配置文件夹",
keys.Combo("C", keys.CmdOrCtrlKey, keys.ShiftKey),
func(_ *menu.CallbackData) {
err := utils.OpenFolder(file.ChyingDir)
if err != nil {
a.diag("Failed to open folder: \n"+err.Error(), true)
return
}
},
),
)),
)
}

// diag ...
func (a *App) diag(message string, error bool, buttons ...string) (string, error) {
if len(buttons) == 0 {
buttons = []string{
conf.BtnConfirmText,
}
}

var t runtime.DialogType

if error {
t = runtime.ErrorDialog
} else {
t = runtime.InfoDialog
}

return runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: t,
Title: conf.Title,
Message: message,
CancelButton: conf.BtnCancelText,
DefaultButton: conf.BtnConfirmText,
Buttons: buttons,
})
}

// startup is called when the app starts. The context is saved ,so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
Expand Down
16 changes: 16 additions & 0 deletions conf/conf.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
package conf

import (
"fmt"
"time"
)

/**
@author: yhy
@since: 2023/4/20
@desc: //TODO
**/

var Proxy string

var Description = fmt.Sprintf("将旦昧爽之交,日夕昏明之际,\n北面而察之,淡淡焉若有物存,莫识其状。\n其所触也,窃窃然有声,经物而物不疾也。\n\n© %d https://github.com/yhy0", time.Now().Year())

const (
Version = "v0.8.0"
Title = "承影 " + Version
VersionNewMsg = "当前已经是最新版本!"
VersionOldMsg = "最新版本: %s, 是否立即更新?"
BtnConfirmText = "确定"
BtnCancelText = "取消"
)
3 changes: 3 additions & 0 deletions frontend/wailsjs/go/main/App.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import {burpSuite} from '../models';
import {menu} from '../models';
import {twj} from '../models';
import {main} from '../models';

Expand All @@ -24,6 +25,8 @@ export function InterceptSend(arg1:string):Promise<void>;

export function Intruder(arg1:string,arg2:string,arg3:Array<string>,arg4:Array<string>,arg5:string,arg6:string):Promise<void>;

export function Menu():Promise<menu.Menu>;

export function Parser(arg1:string):Promise<twj.Jwt>;

export function Proxy(arg1:string):Promise<main.Message>;
Expand Down
4 changes: 4 additions & 0 deletions frontend/wailsjs/go/main/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export function Intruder(arg1, arg2, arg3, arg4, arg5, arg6) {
return window['go']['main']['App']['Intruder'](arg1, arg2, arg3, arg4, arg5, arg6);
}

export function Menu() {
return window['go']['main']['App']['Menu']();
}

export function Parser(arg1) {
return window['go']['main']['App']['Parser'](arg1);
}
Expand Down
11 changes: 4 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package main

import (
"embed"
"fmt"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
"github.com/wailsapp/wails/v2/pkg/options/mac"
"github.com/yhy0/ChYing/pkg/file"
"github.com/yhy0/logging"
"time"
)

//go:embed all:frontend/dist
Expand All @@ -18,7 +16,6 @@ var assets embed.FS
func main() {
logging.New(true, "CY")
file.New()
year := time.Now().Year()
// Create an instance of the app structure
app := NewApp()
// Create application with options
Expand All @@ -32,11 +29,11 @@ func main() {
app,
},
Mac: &mac.Options{
About: &mac.AboutInfo{
Title: "承影",
Message: fmt.Sprintf("将旦昧爽之交,日夕昏明之际,\n北面而察之,淡淡焉若有物存,莫识其状。\n其所触也,窃窃然有声,经物而物不疾也。\n\n© %d https://github.com/yhy0", year),
},
TitleBar: mac.TitleBarHiddenInset(),
WebviewIsTransparent: true,
WindowIsTranslucent: true,
},
Menu: app.Menu(),
})

if err != nil {
Expand Down
30 changes: 15 additions & 15 deletions pkg/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var (
Av map[string]string
)

var chyingDir string
var ChyingDir string

type Rule struct {
Tag string // 文本内容
Expand Down Expand Up @@ -144,13 +144,13 @@ func UserFile() {
// 判断 ChYing 文件夹是否存在
if _, err := os.Stat(filePath); err != nil {
// 不存在,创建
chyingDir = filepath.Join(userCfgDir, "ChYing")
if err := os.MkdirAll(chyingDir, 0755); err != nil {
ChyingDir = filepath.Join(userCfgDir, "ChYing")
if err := os.MkdirAll(ChyingDir, 0755); err != nil {
panic(err)
}
WriteToConfig()
} else {
chyingDir = filepath.Join(userCfgDir, "ChYing")
ChyingDir = filepath.Join(userCfgDir, "ChYing")
// 读取文件
ReadFiles()
}
Expand All @@ -164,12 +164,12 @@ func WriteToConfig() {
if err != nil {
panic(err)
}
if err = os.WriteFile(filepath.Join(chyingDir, "dicc.txt"), content, 0644); err != nil {
if err = os.WriteFile(filepath.Join(ChyingDir, "dicc.txt"), content, 0644); err != nil {
panic(err)
}

// 2. 释放 bbscan 规则文件
bbscan := filepath.Join(chyingDir, "bbscan")
bbscan := filepath.Join(ChyingDir, "bbscan")
if err := os.MkdirAll(bbscan, 0755); err != nil {
panic(err)
}
Expand Down Expand Up @@ -200,12 +200,12 @@ func WriteToConfig() {
if err != nil {
panic(err)
}
if err = os.WriteFile(filepath.Join(chyingDir, "twj.txt"), jwt, 0644); err != nil {
if err = os.WriteFile(filepath.Join(ChyingDir, "twj.txt"), jwt, 0644); err != nil {
panic(err)
}

// 4. 释放 403 bypass 规则文件
bypass := filepath.Join(chyingDir, "403bypass")
bypass := filepath.Join(ChyingDir, "403bypass")
if err = os.MkdirAll(bypass, 0755); err != nil {
panic(err)
}
Expand Down Expand Up @@ -236,14 +236,14 @@ func WriteToConfig() {
if err != nil {
panic(err)
}
if err = os.WriteFile(filepath.Join(chyingDir, "av.json"), av, 0644); err != nil {
if err = os.WriteFile(filepath.Join(ChyingDir, "av.json"), av, 0644); err != nil {
panic(err)
}
}

// ReadFiles 文件存在,读取文件内容
func ReadFiles() {
diccData, err := os.ReadFile(filepath.Join(chyingDir, "dicc.txt"))
diccData, err := os.ReadFile(filepath.Join(ChyingDir, "dicc.txt"))
if err != nil {
logging.Logger.Errorln("ReadFiles(dicc.txt)", err)
diccData, err = fileDicc.ReadFile("dicc.txt")
Expand All @@ -253,7 +253,7 @@ func ReadFiles() {
}
DiccData = strings.Split(string(diccData), "\n")

jwt, err := os.ReadFile(filepath.Join(chyingDir, "twj.txt"))
jwt, err := os.ReadFile(filepath.Join(ChyingDir, "twj.txt"))
if err != nil {
logging.Logger.Errorln("ReadFiles(twj.txt)", err)
diccData, err = jwtSecrets.ReadFile("twj.txt")
Expand All @@ -269,7 +269,7 @@ func ReadFiles() {
regContentType, _ := regexp.Compile(`{type="(.*?)"}`)
regContentTypeNo, _ := regexp.Compile(`{type_no="(.*?)"}`)

bbscan := filepath.Join(chyingDir, "bbscan")
bbscan := filepath.Join(ChyingDir, "bbscan")
entries, err := os.ReadDir(bbscan)
if err != nil {
panic(err)
Expand Down Expand Up @@ -316,7 +316,7 @@ func ReadFiles() {

Bypass403 = make(map[string][]string)
// 返回[]fs.DirEntry
bypass := filepath.Join(chyingDir, "403bypass")
bypass := filepath.Join(ChyingDir, "403bypass")
entries, err = os.ReadDir(bypass)
if err != nil {
panic(err)
Expand All @@ -330,7 +330,7 @@ func ReadFiles() {
Bypass403[entry.Name()] = util.CvtLines(string(content))
}

avfile := filepath.Join(chyingDir, "av.json")
avfile := filepath.Join(ChyingDir, "av.json")
_, err = os.Stat(avfile)
if os.IsNotExist(err) {
av, err := AvFile.ReadFile("av.json")
Expand All @@ -342,7 +342,7 @@ func ReadFiles() {
}
} else {
// 读取 av.json 文件内容
data, err := os.ReadFile(filepath.Join(chyingDir, "av.json"))
data, err := os.ReadFile(filepath.Join(ChyingDir, "av.json"))
if err != nil {
logging.Logger.Errorln("ReadFiles(twj.txt)", err)
data, err = AvFile.ReadFile("av.json")
Expand Down
26 changes: 26 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"math/rand"
"net"
"os"
"os/exec"
"runtime"
"time"
)

Expand Down Expand Up @@ -65,3 +67,27 @@ func Exists(path string) bool {
}
return true
}

func OpenFolder(path string) error {
var err error

switch runtime.GOOS {
case "windows":
err = exec.Command("cmd", "/c", "explorer", path).Start()
case "linux":
err = execCmd("xdg-open", path)
case "darwin":
err = execCmd("open", path)
default:
err = fmt.Errorf("unsupported platform")
}

return err
}

func execCmd(cmd string, args ...string) error {
command := exec.Command(cmd, args...)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
return command.Run()
}

0 comments on commit 407cc05

Please sign in to comment.