Commit 09dd5837 by 陶腾飞

add

parent 96c30b57
No preview for this file type
...@@ -17,10 +17,12 @@ func daemonInit(){ ...@@ -17,10 +17,12 @@ func daemonInit(){
public.PrintLog("init: create Documents Base -> ",public.DocuBase) public.PrintLog("init: create Documents Base -> ",public.DocuBase)
public.Mkdir(public.DocuBase) public.Mkdir(public.DocuBase)
} }
public.PrintLog(public.Env_computername," Deamon start")
if notadserver() { public.SendServerReport(public.Env_computername + " Deamon start")}
} }
func deamonRun(){ func deamonRun(){
daemonHost := public.Env_computername daemonHost := public.Env_computername
public.PrintLog(daemonHost," Deamon Init start")
listener, err := net.Listen("tcp", public.DeamonListen) listener, err := net.Listen("tcp", public.DeamonListen)
if err != nil { if err != nil {
public.PrintLog("Listen Error ", err) public.PrintLog("Listen Error ", err)
...@@ -70,3 +72,11 @@ func main() { ...@@ -70,3 +72,11 @@ func main() {
deamonRun() deamonRun()
} }
func adserver() bool{
if public.Env_MainADHostname == public.Env_computername {return true}
return false
}
func notadserver() bool{
if public.Env_MainADHostname != public.Env_computername {return true}
return false
}
...@@ -34,13 +34,13 @@ const Debug bool = true ...@@ -34,13 +34,13 @@ const Debug bool = true
const DeamonPort string = "16823" const DeamonPort string = "16823"
const DeamonListen string = ":" + DeamonPort const DeamonListen string = ":" + DeamonPort
const ADServerDeamon string = Env_MainADHostname + DeamonListen const ADServerDeamon string = Env_MainADHostname + DeamonListen
const DocuBase string =`C:\Users\Public\Documents\AD-Control\` const DocuBase string = `C:\Users\Public\Documents\AD-Control\`
const RandMax int = 60
// backup env ////////////////////////////////////////////////////////////// // backup env //////////////////////////////////////////////////////////////
var BackupVersion string = "0416" var BackupVersion string = "0417"
var BackupExtFile string = ".ffs_batch" var BackupExtFile string = ".ffs_batch"
var BackupReplaceKey =[]byte("tengfei") var BackupReplaceKey =[]byte("tengfei")
...@@ -67,8 +67,7 @@ const msgerror string = "error" ...@@ -67,8 +67,7 @@ const msgerror string = "error"
// msg type //////////////////////////////////////////////////////////////// // msg type ////////////////////////////////////////////////////////////////
const Msg_Report string = "report" const Msg_Report string = "report"
const Msg_Exec_Shutdown string = "shutdown" const Msg_Exec string = "exec"
const Msg_Exec_Reboot string = "reboot"
const Msg_Backup string = "backup" const Msg_Backup string = "backup"
...@@ -76,7 +75,7 @@ const Msg_Backup string = "backup" ...@@ -76,7 +75,7 @@ const Msg_Backup string = "backup"
// msg report type /////////////////////////////////////////////////////////// // msg report type ///////////////////////////////////////////////////////////
const Msg_Report_Offwork string = "offwork" const Msg_Report_Offwork string = "offwork"
const Msg_Report_OverBak string = "overbak"
// msg backup status ////////////////////////////////////////////////////////////// // msg backup status //////////////////////////////////////////////////////////////
...@@ -85,14 +84,18 @@ const Msg_Backup_Status_Start string = "start" ...@@ -85,14 +84,18 @@ const Msg_Backup_Status_Start string = "start"
const Msg_Backup_Status_RFile string = "rfile" const Msg_Backup_Status_RFile string = "rfile"
const Msg_Backup_Status_If string = "if" const Msg_Backup_Status_If string = "if"
const Msg_Backup_Status_RunBak string = "runbak" const Msg_Backup_Status_RunBak string = "runbak"
const Msg_Backup_Status_Over string = "over" //const Msg_Backup_Status_Over string = "over"
const Msg_Backup_Status_Error string = msgerror const Msg_Backup_Status_Error string = msgerror
// msg exec status //////////////////////////////////////////////////////////////// // msg exec status ////////////////////////////////////////////////////////////////
const Msg_Exec_State_Request string = "request" const Msg_Exec_State_Request string = "request"
const Msg_Exec_State_ACK string = "ack" const Msg_Exec_State_Ack string = "ack"
const Msg_Exec_CloseWindows string = "closewindow"
const Msg_Exec_CloseWindows_cmd string = "shutdown -s -t 15"
const Msg_Exec_CancelCloseWindows string = "cancelclosewindow"
const Msg_Exec_CancelCloseWindows_cmd string = "shutdown -a"
...@@ -103,6 +106,9 @@ type MJbase struct{ ...@@ -103,6 +106,9 @@ type MJbase struct{
//MsgStatus string `json:"msgstatus"` //MsgStatus string `json:"msgstatus"`
Username string `json:"username"` Username string `json:"username"`
Computername string `json:"computername"` Computername string `json:"computername"`
Status string `json:"status"`
Explain string `json:"explain"`
} }
// report Json Format ////////////////////////////////////////////////////// // report Json Format //////////////////////////////////////////////////////
type MJreport struct { type MJreport struct {
...@@ -113,21 +119,19 @@ type MJreport struct { ...@@ -113,21 +119,19 @@ type MJreport struct {
// Exec Json Fromat /////////////////////////////////////////////////////// // Exec Json Fromat ///////////////////////////////////////////////////////
type MJexec struct { type MJexec struct {
MJbase MJbase
Status string `json:"status"` Instruction string `json:instruction`
Data string `json:"data"` Command string `json:"command"`
ExtData string `json:"extdata"` //ExtData string `json:"extdata"`
IfDelay bool `josn:"ifDelay"`
Delay string `json:"delay"` Delay string `json:"delay"`
Explain string `json:"explain"`
} }
// backup Json Format ///////////////////////////////////////////////////// // backup Json Format /////////////////////////////////////////////////////
type MJbackup struct{ type MJbackup struct{
MJbase MJbase
Status string `json:"status"`
Version string `json:"version"` Version string `json:"version"`
DiskFree string `json:"diskfree"` DiskFree string `json:"diskfree"`
Error bool `json:"error"` Error bool `json:"error"`
BatchFileExist bool `json:"batchfileexist"` BatchFileExist bool `json:"batchfileexist"`
BatchFileData []byte `json:"batchfiledata"` BatchFileData []byte `json:"batchfiledata"`
BatchFileName string `json:"batchfilename"` BatchFileName string `json:"batchfilename"`
Explain string `json:"explain"`
} }
package public
import(
"os/exec"
)
func Execcmd_nowait(execfile string,arge string){
PrintLog(execfile," ",arge)
exec.Command(execfile,arge).Start()
}
package public
import (
"flag"
)
func FlagInit()bool{
var reportText = flag.String(Msg_Report,"","report to AD")
var backupText = flag.String(Msg_Backup,"","backup status to AD")
var execText = flag.String(Msg_Exec,"","exec to LocalDaemon")
flag.Parse()
if *reportText != "" {flag_report(reportText);return true}
if *backupText != "" {flag_bakcup(backupText);return true}
if *execText != "" {flag_exec(execText);return true}
return false
}
func flag_report(s *string){
SendServerReport(*s)
}
func flag_bakcup(s *string){
;
}
func flag_exec(s * string){
;
}
\ No newline at end of file
...@@ -9,12 +9,11 @@ func DealMsgEnter(ip string,msgtype string,unmsg []byte) []byte { ...@@ -9,12 +9,11 @@ func DealMsgEnter(ip string,msgtype string,unmsg []byte) []byte {
ParseJsonBody(unmsg,&pmsg) ParseJsonBody(unmsg,&pmsg)
ret = pmsg.Msg_Deal() ret = pmsg.Msg_Deal()
// 执行 消息 // 执行 消息
case Msg_Exec_Shutdown: case Msg_Exec:
var pmsg MJexec var pmsg MJexec
ParseJsonBody(unmsg,&pmsg) ParseJsonBody(unmsg,&pmsg)
//return Msg_exec_shutdown(&pmsg) ret = pmsg.Msg_Deal()
// 备份 消息 // 备份 消息
case Msg_Backup: case Msg_Backup:
...@@ -32,12 +31,17 @@ func DealMsgEnter(ip string,msgtype string,unmsg []byte) []byte { ...@@ -32,12 +31,17 @@ func DealMsgEnter(ip string,msgtype string,unmsg []byte) []byte {
// //
func (rep *MJreport)Msg_Deal() interface{} { func (rep *MJreport)Msg_Deal() interface{} {
PrintLog(rep.Explain)
switch rep.Report{ switch rep.Report{
case Msg_Report_Offwork: case Msg_Report_Offwork:
if IfPrimaryUser(rep.Username){ if IfPrimaryUser(rep.Username){
return GJbackup(Msg_Backup_Status_Start) return GJbackup(Msg_Backup_Status_Start)
} }
return nil return nil
case Msg_Report_OverBak:
return GJexecAck(Msg_Exec_CloseWindows)
default: default:
PrintLog("Default Report Json ",rep.Report) PrintLog("Default Report Json ",rep.Report)
...@@ -45,15 +49,33 @@ func (rep *MJreport)Msg_Deal() interface{} { ...@@ -45,15 +49,33 @@ func (rep *MJreport)Msg_Deal() interface{} {
return nil return nil
} }
// func Msg_exec_shutdown(j *MJexec)interface{}{ func (exec *MJexec)Msg_Deal()interface{}{
// switch j.MsgStatus{ switch exec.Status{
// case Msg_Exec_State_Request: case Msg_Exec_State_Request:
// return j // maybe 当然时我来控制咯
// case Msg_Exec_State_ACK: // 修改 请求状态,client daemon才可以执行
// ; exec.Status = Msg_Exec_State_Ack
// }
// return nil // 根据指令类型
// } // 确定具体命令
// 以及是否延迟
switch exec.Instruction{
case Msg_Exec_CloseWindows:
exec.Command = Msg_Exec_CloseWindows_cmd
exec.IfDelay = false
case Msg_Exec_CancelCloseWindows:
exec.Command = Msg_Exec_CancelCloseWindows_cmd
exec.IfDelay = false
}
return exec
case Msg_Exec_State_Ack:
if exec.IfDelay {Delay(&exec.Delay)}
Execcmd_nowait(exec.Command)
}
return nil
}
func (bak * MJbackup)Msg_Deal () interface{}{ func (bak * MJbackup)Msg_Deal () interface{}{
switch bak.Status{ switch bak.Status{
case Msg_Backup_Status_Start: case Msg_Backup_Status_Start:
...@@ -99,7 +121,6 @@ func (bak * MJbackup)Msg_Deal () interface{}{ ...@@ -99,7 +121,6 @@ func (bak * MJbackup)Msg_Deal () interface{}{
bak.Version = BackupVersion bak.Version = BackupVersion
} }
bak.Status = Msg_Backup_Status_RunBak bak.Status = Msg_Backup_Status_RunBak
bak.Explain = "Backup Runing" bak.Explain = "Backup Runing"
PrintLog(bak.Explain) PrintLog(bak.Explain)
...@@ -118,11 +139,9 @@ func (bak * MJbackup)Msg_Deal () interface{}{ ...@@ -118,11 +139,9 @@ func (bak * MJbackup)Msg_Deal () interface{}{
} }
bak.Explain = "Backup Runing" bak.Explain = "Backup Runing"
PrintLog(bak.Explain) PrintLog(bak.Explain)
Execcmd_nowait(Env_FreeFileSyncExe, bak.BatchFileName) Execcmd_nowait(Env_FreeFileSyncExe + " "+ bak.BatchFileName)
return nil return nil
case Msg_Backup_Status_Over:
PrintLog(" 查询文件以判断是否结束")
case Msg_Backup_Status_Error: case Msg_Backup_Status_Error:
PrintLog(bak.Explain) PrintLog(bak.Explain)
return nil return nil
...@@ -133,19 +152,54 @@ func (bak * MJbackup)Msg_Deal () interface{}{ ...@@ -133,19 +152,54 @@ func (bak * MJbackup)Msg_Deal () interface{}{
} }
//
// 快速 Json 获取
//
func GJreport(s string) interface{}{ func GJreport(s string) interface{}{
var rep MJreport var rep MJreport
rep.init(s) rep.init(s)
return rep return rep
} }
func (rep * MJreport)init(Report string) {
rep.Username = Env_username
rep.Computername = Env_computername
rep.MsgType = Msg_Report
rep.Report = Report
}
func GJexec(s string) interface{}{
var rep MJexec
rep.init(s)
return rep
}
func GJexecAck(s string) interface{}{
// var rep := GJexec(s)
// 这样不行,GJexec返回的是interface{}
var rep MJexec
rep.init(s)
rep.Status = Msg_Exec_State_Ack
return rep
}
func (exec * MJexec)init(s string){
exec.Username = Env_username
exec.Computername = Env_computername
exec.MsgType = Msg_Exec
exec.Instruction = s
//exec.Command = cmd
exec.Status = Msg_Exec_State_Request
exec.IfDelay = false
exec.Delay = Rand(RandMax)
}
func GJbackup(s string)interface{}{ func GJbackup(s string)interface{}{
var rep MJbackup var rep MJbackup
rep.init(s) rep.init(s)
return rep return rep
} }
func (bak * MJbackup)init(Status string) { func (bak * MJbackup)init(Status string) {
bak.Username = Env_username bak.Username = Env_username
bak.Computername = Env_computername bak.Computername = Env_computername
...@@ -154,13 +208,6 @@ func (bak * MJbackup)init(Status string) { ...@@ -154,13 +208,6 @@ func (bak * MJbackup)init(Status string) {
bak.Status = Status bak.Status = Status
} }
func (rep * MJreport)init(Report string) {
rep.Username = Env_username
rep.Computername = Env_computername
rep.MsgType = Msg_Report
rep.Report = Report
}
func (bak * MJbackup)Errorf(s string){ func (bak * MJbackup)Errorf(s string){
bak.Status = Msg_Backup_Status_Error bak.Status = Msg_Backup_Status_Error
bak.Explain = "Backup Error: " + bak.Computername + " -> " + s bak.Explain = "Backup Error: " + bak.Computername + " -> " + s
......
...@@ -6,7 +6,10 @@ import ( ...@@ -6,7 +6,10 @@ import (
"strings" "strings"
"net" "net"
"bufio" "bufio"
"math/rand"
"time"
"io/ioutil" "io/ioutil"
"strconv"
) )
// 获取 环境变量 // 获取 环境变量
...@@ -112,3 +115,12 @@ func IfPrimaryUser(s string) bool{ ...@@ -112,3 +115,12 @@ func IfPrimaryUser(s string) bool{
} }
return false return false
} }
func Rand(i int)string{
rand.Seed(time.Now().UnixNano())
return strconv.Itoa(rand.Intn(i))
}
func Delay(s * string){
DebugLog("延迟",*s,"秒")
i,_ := strconv.Atoi(*s)
time.Sleep(time.Duration(i) * time.Second)
}
\ No newline at end of file
...@@ -16,6 +16,9 @@ func SendServer(v interface{})[]byte{ ...@@ -16,6 +16,9 @@ func SendServer(v interface{})[]byte{
func SendServerReport(s string)[]byte{ func SendServerReport(s string)[]byte{
return SendServer(GJreport(s)) return SendServer(GJreport(s))
} }
func SendServerExec(s string)[]byte{
return SendServer(GJexec(s))
}
func SendMsg(you string ,v interface{})[]byte{ func SendMsg(you string ,v interface{})[]byte{
textbyte,err := json.Marshal(v) textbyte,err := json.Marshal(v)
......
package public
import(
"os/exec"
"github.com/lxn/walk"
"strings"
)
func Execcmd_nowait(cmdargs string){
DebugLog(cmdargs)
// exec.Command
// 只接受 shutdown,-s,-t,0
// 不接受 shutdown -s -t 0
c := strings.Split(cmdargs," ")
len := len(c)
switch len{
case 1:
exec.Command(c[0]).Start()
case 2:
exec.Command(c[0],c[1]).Start()
case 3:
exec.Command(c[0],c[1],c[2]).Start()
case 4:
exec.Command(c[0],c[1],c[2],c[3]).Start()
case 5:
exec.Command(c[0],c[1],c[2],c[3],c[4]).Start()
}
}
func Msg(message * string) {
walk.MsgBox(*new(walk.Form), "提示", *message, walk.MsgBoxIconInformation)
}
\ No newline at end of file
File added
package main package main
import (
"AD-Control/public" import(
"net/http" //"os/exec"
"bytes" "strings"
"fmt"
) )
func main() { func main() {
Content := "Golang Text Bot" cmd := "msg"
scmd := strings.Split(cmd," ")
fmt.Print(len(scmd))
} }
taskkill /f /im users.exe
go build -ldflags="-H windowsgui"
start ./users.exe
\ No newline at end of file
...@@ -15,7 +15,7 @@ import ( ...@@ -15,7 +15,7 @@ import (
func menuInitPower(ni * walk.NotifyIcon){ func menuInitPower(ni * walk.NotifyIcon){
fa := "电源" fa := "电源"
sub := [...]string{"下班咯","关闭系统","用户注销","重启系统"} sub := [...]string{"下班咯","关闭系统","用户注销","重启系统","取消关机计时"}
// 建立空菜单 // 建立空菜单
nm,err := walk.NewMenu() nm,err := walk.NewMenu()
...@@ -40,6 +40,8 @@ func menuInitPower(ni * walk.NotifyIcon){ ...@@ -40,6 +40,8 @@ func menuInitPower(ni * walk.NotifyIcon){
na.Triggered().Attach(pLogOffUser) na.Triggered().Attach(pLogOffUser)
case 4: case 4:
na.Triggered().Attach(pRebootComputer) na.Triggered().Attach(pRebootComputer)
case 5:
na.Triggered().Attach(pCancelCloseWindows)
} }
...@@ -61,18 +63,17 @@ func pWorkOff(){ ...@@ -61,18 +63,17 @@ func pWorkOff(){
public.SendServerReport(public.Msg_Report_Offwork) public.SendServerReport(public.Msg_Report_Offwork)
} }
func pCloseWindows(){ func pCloseWindows(){
//public.SendServerReport(public.Msg_Report_Offwork) public.SendServerExec(public.Msg_Exec_CloseWindows)
public.PrintLog("关闭系统")
} }
func pLogOffUser(){ func pLogOffUser(){
public.PrintLog("注销用户") public.PrintLog("注销用户")
} }
func pRebootComputer(){ func pRebootComputer(){
public.PrintLog("重启主机") public.PrintLog("重启主机")
} }
func pCancelCloseWindows(){
public.SendServerExec(public.Msg_Exec_CancelCloseWindows)
}
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// //
......
taskkill /f /im users.exe taskkill /f /im users.exe
go build -ldflags="-H windowsgui" taskkill /f /im daemon.exe
start ./users.exe start .\users.exe
\ No newline at end of file start ..\daemon\daemon.exe
\ No newline at end of file
No preview for this file type
...@@ -8,6 +8,10 @@ import ( ...@@ -8,6 +8,10 @@ import (
"github.com/lxn/walk" "github.com/lxn/walk"
"AD-Control/public" "AD-Control/public"
) )
func userInit(){
public.SendServerReport("Start User")
}
func menuInit(ni * walk.NotifyIcon){ func menuInit(ni * walk.NotifyIcon){
menuInitPower(ni) menuInitPower(ni)
...@@ -16,27 +20,25 @@ func menuInit(ni * walk.NotifyIcon){ ...@@ -16,27 +20,25 @@ func menuInit(ni * walk.NotifyIcon){
menuInitFileShares(ni) menuInitFileShares(ni)
} }
func userappInit(){
}
func main() { func main() {
//adserver := public.ADServerDeamon // flag 初始化
if public.FlagInit() {return}
public.SendServerReport("Start User") // 用户初始化
userappInit() userInit()
mw, err := walk.NewMainWindow()
// 程序初始化
mw,err := walk.NewMainWindow()
if err != nil { if err != nil {
public.PrintLog(err) public.PrintLog(err)
} }
// We load our icon from a file.
icon, err := walk.Resources.Icon(`AD-Control.ico`) icon, err := walk.Resources.Icon(`AD-Control.ico`)
//icon, err := walk.Resources.Icon(`C:\Windows\SystemApps\Microsoft.Windows.SecHealthUI_cw5n1h2txyewy\Assets\Account.theme-light.ico`)
if err != nil { if err != nil {
public.PrintLog(err) public.PrintLog(err)
} }
// Create the notify icon and make sure we clean it up on exit. // 后台图标应用初始化
ni, err := walk.NewNotifyIcon(mw) ni, err := walk.NewNotifyIcon(mw)
if err != nil { if err != nil {
public.PrintLog(err) public.PrintLog(err)
...@@ -57,10 +59,7 @@ func main() { ...@@ -57,10 +59,7 @@ func main() {
public.PrintLog(err) public.PrintLog(err)
} }
// 循环 运行窗体
// Run the message loop.
mw.Run() mw.Run()
} }
func close(){
public.PrintLog("触发关闭")
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment