加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
starter.go 11.58 KB
一键复制 编辑 原始数据 按行查看 历史
zhijian 提交于 2022-03-09 09:44 . 启动工具隐藏服务地址
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
package main
import (
"strings"
"fmt"
"os"
"os/exec"
"syscall"
"unsafe"
"bufio"
"io"
"errors"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
var TE_server *walk.LineEdit
var TE_user *walk.LineEdit
var TE_pwd *walk.LineEdit
var defaultAddr string = "121.37.226.91:7078" // 服务地址
var defaultExt string = "123" // 用户名
var defaultPwd string = "123" // 密码
var defaultequip string = "0" // 设备编号
var startBtn *walk.PushButton
var saveBtn *walk.PushButton
var mw *walk.MainWindow
var RB_hk *walk.RadioButton
var RB_dh *walk.RadioButton
var dataBinder *walk.DataBinder
type MyDataSource struct {
Etype string
}
func main() {
if GetActiveCount("启动工具.exe") > 1 {
walk.MsgBox(nil, "错误", "程序只能开启一个", walk.MsgBoxOK)
return
}
javacmd := readCmd()
if javacmd == ""{
save(0)
}else{
args,_ := parseCommandLine(javacmd)
for key, value := range args {
if value == "-srv"{
defaultAddr = args[key+1]
}
if value == "-user"{
defaultExt = args[key+1]
}
if value == "-pwd"{
defaultPwd = args[key+1]
}
if value == "-tp"{
defaultequip = args[key+1]
}
}
}
mw.SetMaximizeBox(false)
mw.SetMinimizeBox(true)
mw.SetFixedSize(true)
dataSource := MyDataSource{Etype: "0"}
err := MainWindow{
Title: "配置中转客户端",
AssignTo: &mw,
Size: Size{400, 300},
Layout: VBox{},
Children: []Widget{
Composite{
Layout: VBox{},
Children: []Widget{
Composite{
Layout: VBox{},
Children: []Widget{
Composite{
MaxSize: Size{50, 200},
Layout: VBox{
Margins: Margins{10, 5, 10, 10},
},
Children: []Widget{
/*Composite{
MaxSize: Size{50, 60},
Layout: HBox{
Margins: Margins{0, 0, 0, 5},
},
Children: []Widget{
Label{
Text: "服务地址:",
},
LineEdit{
AssignTo: &TE_server,
Row: 1,
MaxSize: Size{220, 30},
Text: defaultAddr,
},
},
},*/
Composite{
MaxSize: Size{50, 60},
Layout: HBox{
Margins: Margins{0, 0, 0, 5},
},
Children: []Widget{
Label{
Text: "登录账号:",
},
LineEdit{
AssignTo: &TE_user,
Row: 1,
MaxSize: Size{220, 30},
Text: defaultExt,
},
},
},
Composite{
MaxSize: Size{50, 60},
Layout: HBox{
Margins: Margins{0, 0, 0, 5},
},
Children: []Widget{
Label{
Text: "登录密码:",
},
LineEdit{
AssignTo: &TE_pwd,
Row: 1,
MaxSize: Size{220, 30},
Text: defaultPwd,
},
},
},
},
},
},
},
Composite{
Layout: HBox{},
DataBinder: DataBinder{
AssignTo: &dataBinder,
Name: "dataSource",
DataSource: dataSource,
},
Children: []Widget{
Label{
Text: "设备品牌:",
},
RadioButtonGroupBox{
ColumnSpan: 2,
Layout: HBox{},
DataMember: "Etype",
Buttons: []RadioButton{
RadioButton{
Text: "海康威视系列",
Value: "0",
AssignTo: &RB_hk,
},
RadioButton{
Text: "大华系列",
Value: "1",
AssignTo: &RB_dh,
},
},
},
},
},
Composite{
Border: true,
Layout: HBox{},
Children: []Widget{
PushButton{
Text: "保存",
MinSize: Size{80, 60},
MaxSize: Size{80, 60},
AssignTo: &saveBtn,
OnClicked: func() {
go save(1)
},
},
PushButton{
Text: "启动服务",
AssignTo: &startBtn,
MinSize: Size{80, 60},
MaxSize: Size{80, 60},
OnClicked: func() {
startVees()
},
},
},
},
},
},
},
}.Create()
if err != nil {
walk.MsgBox(mw, "错误", "创建失败", walk.MsgBoxOK)
return
}
startBtn.BoundsChanged().Attach(func() {
startBtn.SetY(20)
startBtn.SetHeight(40)
})
saveBtn.BoundsChanged().Attach(func() {
saveBtn.SetY(20)
saveBtn.SetHeight(40)
})
mw.Run()
}
// 事件-----------------------------------------------
func save(s int){
txtAddr := defaultAddr
txtUser := defaultExt
txtPwd := defaultPwd
txtTp := defaultequip
if(s == 1){
//txtAddr = TE_server.Text()
txtUser = TE_user.Text()
txtPwd = TE_pwd.Text()
if RB_hk.Checked(){
txtTp = "0"
}else if RB_dh.Checked(){
txtTp = "1"
}
}
txtcmd := ""
if txtTp == "0"{
txtcmd = "@echo off\r\nstart jre/bin/java -jar transfer-0.0.1-hkvision.jar"
}else if txtTp == "1"{
txtcmd = "@echo off\r\nstart jre/bin/java -jar transfer-0.0.1-dahua.jar"
}
f, err := os.Create("transfer.bat")
if err != nil {
panic(err)
}
defer f.Close()
if len(txtAddr) > 0{
txtcmd = txtcmd + " -srv " + txtAddr
}
if len(txtUser) > 0{
txtcmd = txtcmd + " -user " + txtUser
}
if len(txtPwd) > 0{
txtcmd = txtcmd + " -pwd " + txtPwd
}
if len(txtTp) > 0{
txtcmd = txtcmd + " -tp " + txtTp
}
f.WriteString(txtcmd)
f.Sync()
if(s == 1){
walk.MsgBox(mw, "提示", "保存完成", walk.MsgBoxOK)
}
}
func FileExist(path string) bool {
_, err := os.Lstat(path)
return !os.IsNotExist(err)
}
func startVees() {
if ((defaultequip == "0" && !FileExist("transfer-0.0.1-hkvision.jar")) || (defaultequip == "1" && !FileExist("transfer-0.0.1-dahua.jar"))){
walk.MsgBox(mw, "错误", "请放在JAR包同目录下运行", walk.MsgBoxOK)
return
}
command := "transfer.bat"
params := []string{}
if len(defaultExt) > 1 {
params = append(params, defaultExt)
}
execCommand(command, params)
mw.Close()
}
func execCommand(commandName string, params []string) bool {
cmd := exec.Command(commandName, params...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
cmd.Start()
return true
}
func str2bytes(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
func bytes2str(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
var (
modKernel32 = syscall.NewLazyDLL("kernel32.dll")
procCloseHandle = modKernel32.NewProc("CloseHandle")
procCreateToolhelp32Snapshot = modKernel32.NewProc("CreateToolhelp32Snapshot")
procProcess32First = modKernel32.NewProc("Process32FirstW")
procProcess32Next = modKernel32.NewProc("Process32NextW")
procGetCurrentProcessId = modKernel32.NewProc("GetCurrentProcessId")
)
// Some constants from the Windows API
const (
ERROR_NO_MORE_FILES = 0x12
MAX_PATH = 260
)
// PROCESSENTRY32 is the Windows API structure that contains a process's
// information.
type PROCESSENTRY32 struct {
Size uint32
CntUsage uint32
ProcessID uint32
DefaultHeapID uintptr
ModuleID uint32
CntThreads uint32
ParentProcessID uint32
PriorityClassBase int32
Flags uint32
ExeFile [MAX_PATH]uint16
}
// Process is an implementation of Process for Windows.
type Process struct {
pid int
ppid int
exe string
}
// NewWindowsProcess convert external to internal process data structure
func NewWindowsProcess(e *PROCESSENTRY32) Process {
// Find when the string ends for decoding
end := 0
for {
if e.ExeFile[end] == 0 {
break
}
end++
}
return Process{
pid: int(e.ProcessID),
ppid: int(e.ParentProcessID),
exe: syscall.UTF16ToString(e.ExeFile[:end]),
}
}
// FindProcess will find process by its ID
func FindProcess(pid int) (Process, bool) {
processes, err := ListProcess()
if err == nil {
for _, process := range processes {
if process.pid == pid {
return process, true
}
}
}
return Process{}, false
}
// ListProcess returns list of all active system processes
func ListProcess() ([]Process, error) {
handle, _, _ := procCreateToolhelp32Snapshot.Call(0x00000002, 0)
if handle < 0 {
return nil, syscall.GetLastError()
}
defer procCloseHandle.Call(handle)
entry := PROCESSENTRY32{}
entry.Size = uint32(unsafe.Sizeof(entry))
ret, _, _ := procProcess32First.Call(handle, uintptr(unsafe.Pointer(&entry)))
if ret == 0 {
return nil, fmt.Errorf("Error retrieving process info.")
}
results := make([]Process, 0, 50)
for {
results = append(results, NewWindowsProcess(&entry))
ret, _, _ := procProcess32Next.Call(handle, uintptr(unsafe.Pointer(&entry)))
if ret == 0 {
break
}
}
return results, nil
}
// MapProcess same as ListProcess but returned as map for lookup by processID
func MapProcess() map[int]Process {
procs, _ := ListProcess()
list := make(map[int]Process, len(procs))
for _, proc := range procs {
list[proc.pid] = proc
}
return list
}
// CurrentProcessID return processID of the process that calls this function
func CurrentProcessID() int {
id, _, _ := procGetCurrentProcessId.Call()
return int(id)
}
// IsProcessActive returns true if one of the running processes uses the given executable
func IsProcessActive(exe string) bool {
processes, err := ListProcess()
if err == nil {
for _, process := range processes {
if process.exe == exe {
return true
}
}
}
return false
}
// 获得当前同样的进程的数量
func GetActiveCount(exe string) int {
count := 0
processes, err := ListProcess()
if err == nil {
for _, process := range processes {
if process.exe == exe {
count++
}
}
}
return count
}
func readCmd() string{
fi, err := os.Open("transfer.bat")
if err != nil {
fmt.Printf("Error: %s\n", err)
return ""
}
defer fi.Close()
br := bufio.NewReader(fi)
for {
a, _, c := br.ReadLine()
if c == io.EOF {
break
}
cmdstr := string(a)
if(strings.Contains(cmdstr, "java")){
return cmdstr
}
}
return ""
}
// 解析命令行
func parseCommandLine(command string) ([]string, error) {
var args []string
state := "start"
current := ""
quote := "\""
escapeNext := true
for i := 0; i < len(command); i++ {
c := command[i]
if state == "quotes" {
if string(c) != quote {
current += string(c)
} else {
args = append(args, current)
current = ""
state = "start"
}
continue
}
if (escapeNext) {
current += string(c)
escapeNext = false
continue
}
if (c == '\\') {
escapeNext = true
continue
}
if c == '"' || c == '\'' {
state = "quotes"
quote = string(c)
continue
}
if state == "arg" {
if c == ' ' || c == '\t' {
args = append(args, current)
current = ""
state = "start"
} else {
current += string(c)
}
continue
}
if c != ' ' && c != '\t' {
state = "arg"
current += string(c)
}
}
if state == "quotes" {
return []string{}, errors.New(fmt.Sprintf("Unclosed quote in command line: %s", command))
}
if current != "" {
args = append(args, current)
}
return args, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化