diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | LICENSE | 9 | ||||
-rw-r--r-- | Makefile | 12 | ||||
-rw-r--r-- | Readme.textile | 21 | ||||
-rw-r--r-- | become.go | 88 | ||||
-rw-r--r-- | becomings/Makefile | 2 | ||||
-rw-r--r-- | becomings/test.go | 11 |
7 files changed, 145 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..829a2c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +become +becomings/*.so @@ -0,0 +1,9 @@ +Copyright 2019 Nat Lasseter + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b0844a4 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +become: become.go + go build + + +.PHONY: install no-really-install + +install: become + @echo "Don't be an idiot." + +no-really-install: become + @echo "Dumbass." + go install diff --git a/Readme.textile b/Readme.textile new file mode 100644 index 0000000..a2c2e67 --- /dev/null +++ b/Readme.textile @@ -0,0 +1,21 @@ +h1. Go Become Server + +p. Rest in peace, Joe. + +h2. Server + +# make +# ./become + +The server will start listening on tcp://localhost:3333. + +h2. Client + +# cd becomings +# make test.so +# cat test.so | nc localhost 3333 +# ^C + +h2. Result + +p. The server will receive the plugin and become it. diff --git a/become.go b/become.go new file mode 100644 index 0000000..61bfbb9 --- /dev/null +++ b/become.go @@ -0,0 +1,88 @@ +package main + +import ( + "fmt" + "io" + "io/ioutil" + "net" + "os" + "plugin" + "sync" +) + +const ( + LISTEN = "localhost:3333" +) + +var ( + wait sync.WaitGroup +) + +type Become interface { + Become() +} + +func main() { + l, err := net.Listen("tcp", LISTEN) + if err != nil { + fmt.Println("Error listening: ", err.Error()) + os.Exit(1) + } + defer l.Close() + conn, err := l.Accept() + if err != nil { + fmt.Println("Error accepting: ", err.Error()) + } else { + wait.Add(1) + go handleRequest(conn) + } + wait.Wait() +} + +func handleRequest(conn net.Conn) { + defer wait.Done() + defer conn.Close() + file, err := ioutil.TempFile("", "become.*") + if err != nil { + fmt.Println("Error creating temporary file: ", err.Error()) + return + } + _, err = io.Copy(file, conn) + if err != nil { + fmt.Println("Error copying: ", err.Error()) + return + } + err = file.Close() + if err != nil { + fmt.Println("Error closing temporary file: ", err.Error()) + return + } + wait.Add(1) + go becomePlugin(file.Name()) +} + +func becomePlugin(filename string) { + defer wait.Done() + defer os.Remove(filename) + + plugin, err := plugin.Open(filename) + if err != nil { + fmt.Println("Error opening become: ", err.Error()) + return + } + + symBecome, err := plugin.Lookup("Become") + if err != nil { + fmt.Println("Error looking up become: ", err.Error()) + return + } + + var become Become + become, ok := symBecome.(Become) + if !ok { + fmt.Println("Become not of correct type") + return + } + + become.Become() +} diff --git a/becomings/Makefile b/becomings/Makefile new file mode 100644 index 0000000..8533307 --- /dev/null +++ b/becomings/Makefile @@ -0,0 +1,2 @@ +%.so: %.go + go build -buildmode=plugin -o $@ $< diff --git a/becomings/test.go b/becomings/test.go new file mode 100644 index 0000000..5e68e40 --- /dev/null +++ b/becomings/test.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +type becomer string + +func (b becomer) Become() { + fmt.Println("Success!") +} + +var Become becomer |