The simple "Trojan" applet implemented by the Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Beginners go language may feel very boring, and go and other programming language syntax is very different, it may be difficult to get started, in order to improve the learning of the go language interest, quickly grasp the basic grammar of go, I tried to write a similar back-door applet, The main function is to be able to view and manipulate the target device on another device, I am running this applet on my own computer, then I use the pad to operate my computer, to achieve simple directory access and file viewing, as well as the simple function of restarting or shutting down the computer, because I also started to learn the second day of the Go language, Also on the first day of the night to see the grammar of Go, so the process of reading a lot of information on the Internet, My Learning idea is to first find keywords from the network, and then to check the corresponding API documentation, and then refer to the document and their understanding to accomplish this small goal.

In order to facilitate the study of reference, I put the final code out, on two files, the first file is as follows, the Vince package has two methods, one is read all the files under the path, and the other way is to output the file in text mode

Package Vinceimport ("Io/ioutil"//io Action Pack "OS"//System Toolkit) Func Listdir (Dirpath string) (Files []string, err error) {files = make ([]string,0,10)//initialize array, length is 0, initial capacity is 10, capacity will increase with actual dir,err: = Ioutil. ReadDir (Dirpath)//Read the specified path if err! = Nil {//If there is an exception returns an exception return NIL,ERR}PATHSEP: = string (OS. PathSeparator)//system path delimiter for _,file: = range dir {//traversal path list (if I change to _ then you can use the FMT package) files = Append (Files,dirpath + pathsep + file. Name () + "\ n")//stitching the path and adding the array to the}return files,nil//returns the file list}func OpenFile (FilePath string) (Datas string) {data,err: = Ioutil. ReadFile (FilePath)//Open the specified file if err! = Nil {return "" "}return string (data)//return file contents as text}


The second is the program's entry program, but also includes a Web service to provide functionality, in order to facilitate remote operation

Package Mainimport ("FMT"//Format Pack "log" "net/http"//"Strings"//String Toolkit "./vince" "os/exec") type Hello struct{}func (h hello) Servehttp (w http. Responsewriter, R *http. Request) {r.parseform () var filePath string//= "/users/vince" var fileName string//= "/users/vince/test.xml" var cmds stri Ngfor k, V: = range R.form {//Traverse request form parameter to if strings. Containsany (k, "ls") {//If you are viewing the path filepath = strings. Join (V, "")}if strings. Containsany (K, "PT") {//If you are viewing the file filename = strings. Join (V, "")}if strings. Containsany (k, "cmd") {//If the command is to be executed CMDS = strings. Join (V, "") if Cmds = = "reboot" {//Is the reboot command FMT. PRINTLN ("reboot") cmd: = Exec.command ("Shutdown", "-R", "Now")//Generate restart command cmd. Start ()//Start execution}}}if FilePath! = "" {//Output file list FMT. Fprint (W, "The Path is:") fmt. Fprint (W, FilePath) fmt. Fprint (w, "\ n") Files,err: = Vince. Listdir (FilePath) if err! = Nil {fmt. Fprint (W, err)}fmt. Fprint (w, files)}if filename! = "" {//Output the file contents in text mode FMT. Fprint (w, "\ n") fmt. Fprint (W, "The file is:") fmt. Fprint (W, fileName) fmt. Fprint (w, "\ n") Datas: = Vince. OpenFile (FileName) FMt. Fprint (W, datas)}}//Program entry func main () {var hs hello//listen 8080 port err: = http. Listenandserve ("192.168.1.103:8080", HS) if err! = Nil {log. Fatal (ERR)}}


The IP here is my computer in the LAN, why not 127.0.0.1 it?  If you use 127.0.0.1, my pad will not be able to access, because 127.0.0.1 is a computer on their own virtual IP, LAN is not accessible, here the 8080 port is can be specified, but also with other ports such as 7070, run if the command is found to not execute, you can use sudo go run web.go mode because permissions are required to execute the command.

Also attach the operation on the pad to view the contents of the 1.txt file:


In fact, I write this applet is also to let my wife (test novice) can better get started with the go language, I am a Java programmer, if the beginner go language has doubts, can go to https://golang.org/dl/and https://tour.go-zh.org/ BASICS/6 find, with detailed instructions for installation and syntax

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.