Commit 09dd5837 by 陶腾飞

add

parent 96c30b57
No preview for this file type
......@@ -17,10 +17,12 @@ func daemonInit(){
public.PrintLog("init: create Documents Base -> ",public.DocuBase)
public.Mkdir(public.DocuBase)
}
public.PrintLog(public.Env_computername," Deamon start")
if notadserver() { public.SendServerReport(public.Env_computername + " Deamon start")}
}
func deamonRun(){
daemonHost := public.Env_computername
public.PrintLog(daemonHost," Deamon Init start")
listener, err := net.Listen("tcp", public.DeamonListen)
if err != nil {
public.PrintLog("Listen Error ", err)
......@@ -70,3 +72,11 @@ func main() {
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
}
......@@ -2,53 +2,53 @@ package public
// user env ///////////////////////////////////////////////////////////////
var Env_dept string = GetEnvif("DEPT")
var Env_userprofile string = GetEnvif("USERPROFILE")
var Env_username string = GetEnvif("USERNAME")
var Env_computername string = GetEnvif("COMPUTERNAME")
var Env_homeshare string = GetEnvif("HOMESHARE")
var Env_userdomain string = GetEnvif("USERDOMAIN")
var Env_dept string = GetEnvif("DEPT")
var Env_userprofile string = GetEnvif("USERPROFILE")
var Env_username string = GetEnvif("USERNAME")
var Env_computername string = GetEnvif("COMPUTERNAME")
var Env_homeshare string = GetEnvif("HOMESHARE")
var Env_userdomain string = GetEnvif("USERDOMAIN")
// computer env /////////////////////////////////////////////////////
const Env_FreeFileSyncExe string = `C:\Program Files (x86)\zhiwei\FreeFileSync_10.22\FreeFileSync.exe`
const Env_FreeFileSyncExe string = `C:\Program Files (x86)\zhiwei\FreeFileSync_10.22\FreeFileSync.exe`
// AD env ///////////////////////////////////////////////////////////
const Env_MainADHostname string = "ADSERVER"
const Env_SMBAdserver string = `\\` + Env_MainADHostname + `\`
const Env_ADSoft string = Env_SMBAdserver + "software"
const Env_ADMain string = Env_SMBAdserver + "ADMAIN"
var Env_ADMainDeptShare string = Env_ADMain + `\` + Env_dept + `\` + "部门共享"
const Env_ADSfoftlike string = Env_ADSoft + `\` + "softlike"
const Env_MainADHostname string = "ADSERVER"
const Env_SMBAdserver string = `\\` + Env_MainADHostname + `\`
const Env_ADSoft string = Env_SMBAdserver + "software"
const Env_ADMain string = Env_SMBAdserver + "ADMAIN"
var Env_ADMainDeptShare string = Env_ADMain + `\` + Env_dept + `\` + "部门共享"
const Env_ADSfoftlike string = Env_ADSoft + `\` + "softlike"
// app env ////////////////////////////////////////////////////////////////
const Debug bool = true
const DeamonPort string = "16823"
const DeamonListen string = ":" + DeamonPort
const ADServerDeamon string = Env_MainADHostname + DeamonListen
const DocuBase string =`C:\Users\Public\Documents\AD-Control\`
const Debug bool = true
const DeamonPort string = "16823"
const DeamonListen string = ":" + DeamonPort
const ADServerDeamon string = Env_MainADHostname + DeamonListen
const DocuBase string = `C:\Users\Public\Documents\AD-Control\`
const RandMax int = 60
// backup env //////////////////////////////////////////////////////////////
var BackupVersion string = "0416"
var BackupExtFile string = ".ffs_batch"
var BackupVersion string = "0417"
var BackupExtFile string = ".ffs_batch"
var BackupReplaceKey =[]byte("tengfei")
// wxwork env ///////////////////////////////////////////////////////////////
const WxworkBotLink string = `https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=59107f72-5b72-4a20-a33c-fdb1cd46f2c6`
const WxworkBotLink string = `https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=59107f72-5b72-4a20-a33c-fdb1cd46f2c6`
//
//
......@@ -60,74 +60,78 @@ const WxworkBotLink string = `https://qyapi.weixin.qq.com/cgi-bin/webhook/sen
// msg env ////////////////////////////////////////////////////////////////
const EndSign byte = 125
const msgerror string = "error"
const EndSign byte = 125
const msgerror string = "error"
// msg type ////////////////////////////////////////////////////////////////
const Msg_Report string = "report"
const Msg_Exec_Shutdown string = "shutdown"
const Msg_Exec_Reboot string = "reboot"
const Msg_Backup string = "backup"
const Msg_Report string = "report"
const Msg_Exec string = "exec"
const Msg_Backup string = "backup"
// msg report type ///////////////////////////////////////////////////////////
const Msg_Report_Offwork string = "offwork"
const Msg_Report_Offwork string = "offwork"
const Msg_Report_OverBak string = "overbak"
// msg backup status //////////////////////////////////////////////////////////////
const Msg_Backup_Status_Start string = "start"
const Msg_Backup_Status_RFile string = "rfile"
const Msg_Backup_Status_If string = "if"
const Msg_Backup_Status_RunBak string = "runbak"
const Msg_Backup_Status_Over string = "over"
const Msg_Backup_Status_Error string = msgerror
const Msg_Backup_Status_Start string = "start"
const Msg_Backup_Status_RFile string = "rfile"
const Msg_Backup_Status_If string = "if"
const Msg_Backup_Status_RunBak string = "runbak"
//const Msg_Backup_Status_Over string = "over"
const Msg_Backup_Status_Error string = msgerror
// msg exec status ////////////////////////////////////////////////////////////////
const Msg_Exec_State_Request string = "request"
const Msg_Exec_State_ACK string = "ack"
const Msg_Exec_State_Request string = "request"
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"
// Basic Json Fromat ///////////////////////////////////////////////////////
type MJbase struct{
MsgType string `json:"msgtype"`
//MsgStatus string `json:"msgstatus"`
Username string `json:"username"`
Computername string `json:"computername"`
MsgType string `json:"msgtype"`
//MsgStatus string `json:"msgstatus"`
Username string `json:"username"`
Computername string `json:"computername"`
Status string `json:"status"`
Explain string `json:"explain"`
}
// report Json Format //////////////////////////////////////////////////////
type MJreport struct {
MJbase
Report string `json:"report"`
Report string `json:"report"`
}
// Exec Json Fromat ///////////////////////////////////////////////////////
type MJexec struct {
MJbase
Status string `json:"status"`
Data string `json:"data"`
ExtData string `json:"extdata"`
Delay string `json:"delay"`
Explain string `json:"explain"`
Instruction string `json:instruction`
Command string `json:"command"`
//ExtData string `json:"extdata"`
IfDelay bool `josn:"ifDelay"`
Delay string `json:"delay"`
}
// backup Json Format /////////////////////////////////////////////////////
type MJbackup struct{
MJbase
Status string `json:"status"`
Version string `json:"version"`
DiskFree string `json:"diskfree"`
Error bool `json:"error"`
BatchFileExist bool `json:"batchfileexist"`
BatchFileData []byte `json:"batchfiledata"`
BatchFileName string `json:"batchfilename"`
Explain string `json:"explain"`
Version string `json:"version"`
DiskFree string `json:"diskfree"`
Error bool `json:"error"`
BatchFileExist bool `json:"batchfileexist"`
BatchFileData []byte `json:"batchfiledata"`
BatchFileName string `json:"batchfilename"`
}
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 {
ParseJsonBody(unmsg,&pmsg)
ret = pmsg.Msg_Deal()
// 执行 消息
case Msg_Exec_Shutdown:
case Msg_Exec:
var pmsg MJexec
ParseJsonBody(unmsg,&pmsg)
//return Msg_exec_shutdown(&pmsg)
ret = pmsg.Msg_Deal()
// 备份 消息
case Msg_Backup:
......@@ -32,12 +31,17 @@ func DealMsgEnter(ip string,msgtype string,unmsg []byte) []byte {
//
func (rep *MJreport)Msg_Deal() interface{} {
PrintLog(rep.Explain)
switch rep.Report{
case Msg_Report_Offwork:
if IfPrimaryUser(rep.Username){
return GJbackup(Msg_Backup_Status_Start)
}
return nil
case Msg_Report_OverBak:
return GJexecAck(Msg_Exec_CloseWindows)
default:
PrintLog("Default Report Json ",rep.Report)
......@@ -45,15 +49,33 @@ func (rep *MJreport)Msg_Deal() interface{} {
return nil
}
// func Msg_exec_shutdown(j *MJexec)interface{}{
// switch j.MsgStatus{
// case Msg_Exec_State_Request:
// return j
// case Msg_Exec_State_ACK:
// ;
// }
// return nil
// }
func (exec *MJexec)Msg_Deal()interface{}{
switch exec.Status{
case Msg_Exec_State_Request:
// maybe 当然时我来控制咯
// 修改 请求状态,client daemon才可以执行
exec.Status = Msg_Exec_State_Ack
// 根据指令类型
// 确定具体命令
// 以及是否延迟
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{}{
switch bak.Status{
case Msg_Backup_Status_Start:
......@@ -99,7 +121,6 @@ func (bak * MJbackup)Msg_Deal () interface{}{
bak.Version = BackupVersion
}
bak.Status = Msg_Backup_Status_RunBak
bak.Explain = "Backup Runing"
PrintLog(bak.Explain)
......@@ -118,11 +139,9 @@ func (bak * MJbackup)Msg_Deal () interface{}{
}
bak.Explain = "Backup Runing"
PrintLog(bak.Explain)
Execcmd_nowait(Env_FreeFileSyncExe, bak.BatchFileName)
Execcmd_nowait(Env_FreeFileSyncExe + " "+ bak.BatchFileName)
return nil
case Msg_Backup_Status_Over:
PrintLog(" 查询文件以判断是否结束")
case Msg_Backup_Status_Error:
PrintLog(bak.Explain)
return nil
......@@ -133,19 +152,54 @@ func (bak * MJbackup)Msg_Deal () interface{}{
}
//
// 快速 Json 获取
//
func GJreport(s string) interface{}{
var rep MJreport
rep.init(s)
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{}{
var rep MJbackup
rep.init(s)
return rep
}
func (bak * MJbackup)init(Status string) {
bak.Username = Env_username
bak.Computername = Env_computername
......@@ -154,13 +208,6 @@ func (bak * MJbackup)init(Status string) {
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){
bak.Status = Msg_Backup_Status_Error
bak.Explain = "Backup Error: " + bak.Computername + " -> " + s
......
......@@ -6,7 +6,10 @@ import (
"strings"
"net"
"bufio"
"math/rand"
"time"
"io/ioutil"
"strconv"
)
// 获取 环境变量
......@@ -111,4 +114,13 @@ func IfPrimaryUser(s string) bool{
return true
}
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{
func SendServerReport(s string)[]byte{
return SendServer(GJreport(s))
}
func SendServerExec(s string)[]byte{
return SendServer(GJexec(s))
}
func SendMsg(you string ,v interface{})[]byte{
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
import (
"AD-Control/public"
"net/http"
"bytes"
)
func main() {
Content := "Golang Text Bot"
import(
//"os/exec"
"strings"
"fmt"
)
}
\ No newline at end of file
func main() {
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 (
func menuInitPower(ni * walk.NotifyIcon){
fa := "电源"
sub := [...]string{"下班咯","关闭系统","用户注销","重启系统"}
sub := [...]string{"下班咯","关闭系统","用户注销","重启系统","取消关机计时"}
// 建立空菜单
nm,err := walk.NewMenu()
......@@ -40,6 +40,8 @@ func menuInitPower(ni * walk.NotifyIcon){
na.Triggered().Attach(pLogOffUser)
case 4:
na.Triggered().Attach(pRebootComputer)
case 5:
na.Triggered().Attach(pCancelCloseWindows)
}
......@@ -61,18 +63,17 @@ func pWorkOff(){
public.SendServerReport(public.Msg_Report_Offwork)
}
func pCloseWindows(){
//public.SendServerReport(public.Msg_Report_Offwork)
public.PrintLog("关闭系统")
public.SendServerExec(public.Msg_Exec_CloseWindows)
}
func pLogOffUser(){
public.PrintLog("注销用户")
}
func pRebootComputer(){
public.PrintLog("重启主机")
}
func pCancelCloseWindows(){
public.SendServerExec(public.Msg_Exec_CancelCloseWindows)
}
//--------------------------------------------------------------------
//
......
taskkill /f /im users.exe
go build -ldflags="-H windowsgui"
start ./users.exe
\ No newline at end of file
taskkill /f /im daemon.exe
start .\users.exe
start ..\daemon\daemon.exe
\ No newline at end of file
No preview for this file type
......@@ -8,6 +8,10 @@ import (
"github.com/lxn/walk"
"AD-Control/public"
)
func userInit(){
public.SendServerReport("Start User")
}
func menuInit(ni * walk.NotifyIcon){
menuInitPower(ni)
......@@ -16,27 +20,25 @@ func menuInit(ni * walk.NotifyIcon){
menuInitFileShares(ni)
}
func userappInit(){
}
func main() {
//adserver := public.ADServerDeamon
// flag 初始化
if public.FlagInit() {return}
// 用户初始化
userInit()
public.SendServerReport("Start User")
userappInit()
mw, err := walk.NewMainWindow()
// 程序初始化
mw,err := walk.NewMainWindow()
if err != nil {
public.PrintLog(err)
}
// We load our icon from a file.
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 {
public.PrintLog(err)
}
// Create the notify icon and make sure we clean it up on exit.
// 后台图标应用初始化
ni, err := walk.NewNotifyIcon(mw)
if err != nil {
public.PrintLog(err)
......@@ -56,11 +58,8 @@ func main() {
if err := ni.SetVisible(true); err != nil {
public.PrintLog(err)
}
// Run the message loop.
// 循环 运行窗体
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