Skip to content

Commit

Permalink
Version 0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
KrxkGit committed Nov 3, 2023
1 parent 83757d6 commit 71d0450
Show file tree
Hide file tree
Showing 15 changed files with 363 additions and 14 deletions.
1 change: 1 addition & 0 deletions Output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Startup
216 changes: 216 additions & 0 deletions SSHCommand/RunSSH.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package SSHCommand

import (
"encoding/json"
"fmt"
"github.com/labstack/gommon/log"
"golang.org/x/crypto/ssh"
"io"
"os"
)

type ScutInfo struct {
Username, Password string
AcIP, AcName string
}

type SSHClass struct {
client *ssh.Client
session *ssh.Session
err error
status bool // 记录上一操作是否成功
out io.Writer // 用于保存输出SSH运行结果
ScutInfo // 保存信息
}

func (s *SSHClass) SetOut(out io.Writer) {
// 先于 NewSession 被调用
s.out = out
}

func (s *SSHClass) NewSession() {
// 每条命令都需要一个新 Session
if s.status != true {
log.Errorf("Dial Fails: %s", s.err.Error())
return
}
s.session, s.err = s.client.NewSession()
s.Assert()

// 设置输出
s.session.Stdout = s.out
}

func (s *SSHClass) Close() {
if s.session != nil {
s.session.Close()
s.session = nil
s.status = true
}
}

func (s *SSHClass) Assert() {
if s.err != nil {
log.Error(s.err.Error())
s.status = false
return
}
s.status = true
}

func (s *SSHClass) RunCommand(cmd string) {
s.NewSession()
defer s.Close()

if s.status != true {
log.Errorf("New Session Fails: %s", s.err.Error())
return
}
err := s.session.Run(cmd)
if err != nil {
log.Error(err.Error())
}
}

type accountInfo struct {
Username, Password string
AcIP, AcName string
}

func (s *SSHClass) preLoadInfo() {
// 遇加载信息
infoFile, err := os.ReadFile("./Account.json")
if err != nil {
log.Info(err.Error())
return
}
accInfo := new(accountInfo)
json.Unmarshal(infoFile, accInfo)

s.Username = accInfo.Username
s.Password = accInfo.Password
s.AcIP = accInfo.AcIP
s.AcName = accInfo.AcName
}

func (s *SSHClass) saveInfo() {
infoFile, err := os.Create("./Account.json")
if err != nil {
log.Error(err.Error())
}

accInfo := new(accountInfo)
accInfo.Username = s.Username
accInfo.Password = s.Password
accInfo.AcIP = s.AcIP
accInfo.AcName = s.AcName

b, err := json.Marshal(accInfo)
if err != nil {
log.Error(err.Error())
}
infoFile.Write(b)
infoFile.Close()
}

func (s *SSHClass) RunSetScutInfo(username, password string) {
log.Info("RunSetScutInfo is called")
s.preLoadInfo()

s.Username = username
s.Password = password
s.HelpSetInfo()
}

func (s *SSHClass) RunSetAcInfo(acIP, acName string) {
log.Info("RunSetAcInfo is called")
s.preLoadInfo()

s.AcIP = acIP
s.AcName = acName
s.HelpSetInfo()
}

func (s *SSHClass) RunWiredLogin() {
cmd := fmt.Sprintf("chmod +x scutlogin.sh && ./scutlogin.sh")
log.Info("RunWiredLogin is called")
s.RunCommand(cmd)
}

func (s *SSHClass) RunWirelessLogin() {
cmd := fmt.Sprintf("chmod +x wl_login.sh && ./wl_login.sh")
log.Info("RunWirelessLogin is called")
s.RunCommand(cmd)
}

func (s *SSHClass) HelpSetInfo() {
// 辅助函数
s.saveInfo()

cmd := fmt.Sprintf("echo %s > SCUT_info.txt", s.Username)
cmd += fmt.Sprintf(" && echo %s >> SCUT_info.txt", s.Password)
cmd += fmt.Sprintf(" && echo %s >> SCUT_info.txt", s.AcIP)
cmd += fmt.Sprintf(" && echo %s >> SCUT_info.txt", s.AcName)
s.RunCommand(cmd)
}

func (s *SSHClass) RunSyncTime() {
cmd := fmt.Sprintf("ntpd -n -q -p ntp.aliyun.com")

log.Info("RunSyncTime is called")
s.RunCommand(cmd)
}

func (s *SSHClass) RunAutoLogin() {
log.Info("RunAutoLogin is called")

cmd := fmt.Sprintf("crontab -l | sed '/.*\\/root\\/scutlogin.sh.*/d' > temp_cron")
cmd += fmt.Sprintf(" && echo \"5 6 * * 1-5 /root/scutlogin.sh\" >> temp_cron")
cmd += fmt.Sprintf(" && crontab temp_cron\nrm temp_cron")

s.RunCommand(cmd)
}

func (s *SSHClass) CancelAutoLogin() {
log.Info("CancelAutoLogin is called")

cmd := fmt.Sprintf("crontab -l | sed '/.*\\/root\\/scutlogin.sh.*/d' > temp_cron")
cmd += fmt.Sprintf(" && crontab temp_cron\nrm temp_cron")
s.RunCommand(cmd)
}

type routerConfig struct {
Addr string `json:"addr"`
Password string `json:"password"`
Port string `json:"port"`
}

func NewRunSSH() *SSHClass {
buf, err := os.ReadFile("./config.json")
if err != nil {
log.Error(err.Error())
f, _ := os.Create("./config.json")
f.Close()
return nil
}

rConfig := new(routerConfig)
err = json.Unmarshal(buf, rConfig)
if err != nil {
log.Error(err.Error())
return nil
}

config := ssh.ClientConfig{
Config: ssh.Config{},
User: "root",
Auth: []ssh.AuthMethod{ssh.Password(rConfig.Password)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
SSHObj := new(SSHClass)
addrPort := fmt.Sprintf("%s:%s", rConfig.Addr, rConfig.Port)
SSHObj.client, SSHObj.err = ssh.Dial("tcp", addrPort, &config)
SSHObj.Assert()

return SSHObj
}
Empty file added ScutLog.log
Empty file.
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"addr":"192.168.1.101",
"password": "KrxkKrxk",
"port": "22"
}
2 changes: 1 addition & 1 deletion frontend/src/components/Demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<MyAvatar/>
</Header>
<Content class="layout-content">
<h2>自由定制,真正属于你的校园网路由器</h2>
<h2>自由定制,打造您的专属校园网路由</h2>
<img src="../assets/Scut-Router.png"/>
</Content>
<Footer class="layout-footer">
Expand Down
15 changes: 10 additions & 5 deletions frontend/src/components/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
</div>
</template>
<script>
import {RunAutoLogin, CancelAutoLogin, RunSetScutInfo} from "../../wailsjs/go/SSHCommand/SSHClass";
export default {
data() {
return {
Expand All @@ -27,11 +29,14 @@ export default {
},
methods: {
handleSubmit(valid, { username, password }) {
if (valid) {
this.$Modal.info({
title: '输入的内容如下:',
content: `username: ${username} | password: ${password} | type: ${this.AccountType}`,
})
if(valid) {
RunSetScutInfo(username, password)
if(this.AccountType == "本科生") {
RunAutoLogin()
} else {
CancelAutoLogin()
}
alert('完成')
}
},
},
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/views/ACInfo.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script>
import MyMenu from '../components/Menu.vue'
import {RunSetAcInfo} from "../../wailsjs/go/SSHCommand/SSHClass";
const ACList = [] // 复合型const变量只确保地址不变而非值不变,故可修改
for (let i = 1; i < 20; i += 1) {
Expand All @@ -20,7 +21,9 @@ export default {
},
methods: {
OnSubmit() {
alert(`Wlan_ac_name: ${this.AcName}\nWlan_ac_ip: ${this.AcIp}`)
RunSetAcInfo(this.AcIp, this.AcName)
alert('完成')
// alert(`Wlan_ac_name: ${this.AcName}\nWlan_ac_ip: ${this.AcIp}`)
}
},
name: 'ACInfo'
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/views/SyncTime.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import MyMenu from '../components/Menu.vue'
import Success from "../components/Success.vue"
import Fail from "../components/Fail.vue"
import {RunSyncTime} from "../../wailsjs/go/SSHCommand/SSHClass";
export default {
components: {
Expand All @@ -18,8 +19,9 @@ export default {
},
methods: {
TryConnect() {
RunSyncTime()
this.SyncStatus = true
this.FailStatus = true
// this.FailStatus = true
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/views/WiredLogin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import MyMenu from '../components/Menu.vue'
import Success from "../components/Success.vue"
import Fail from "../components/Fail.vue"
import {RunWiredLogin} from "../../wailsjs/go/SSHCommand/SSHClass";
export default {
components: {
Expand All @@ -18,8 +19,9 @@ export default {
},
methods: {
TryConnect() {
RunWiredLogin()
this.ConnectStatus = true
this.FailStatus = true
// this.FailStatus = true
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/views/WirelessLogin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import MyMenu from '../components/Menu.vue'
import Success from '../components/Success.vue'
import Fail from "../components/Fail.vue"
import {RunWirelessLogin} from "../../wailsjs/go/SSHCommand/SSHClass";
export default {
components: {
Expand All @@ -18,8 +19,9 @@ export default {
},
methods: {
TryConnect() {
RunWirelessLogin()
this.ConnectStatus = true
this.FailStatus = true
// this.FailStatus = true
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions frontend/wailsjs/go/SSHCommand/SSHClass.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import {io} from '../models';

export function Assert():Promise<void>;

export function CancelAutoLogin():Promise<void>;

export function Close():Promise<void>;

export function HelpSetInfo():Promise<void>;

export function NewSession():Promise<void>;

export function RunAutoLogin():Promise<void>;

export function RunCommand(arg1:string):Promise<void>;

export function RunSetAcInfo(arg1:string,arg2:string):Promise<void>;

export function RunSetScutInfo(arg1:string,arg2:string):Promise<void>;

export function RunSyncTime():Promise<void>;

export function RunWiredLogin():Promise<void>;

export function RunWirelessLogin():Promise<void>;

export function SetOut(arg1:io.Writer):Promise<void>;
Loading

0 comments on commit 71d0450

Please sign in to comment.