|
// +build ignore |
|
|
|
package main |
|
|
|
import ( |
|
"flag" |
|
"fmt" |
|
"net" |
|
"net/http" |
|
"strconv" |
|
"strings" |
|
"time" |
|
) |
|
|
|
var ( |
|
httpPort string |
|
) |
|
|
|
func init() { |
|
flag.StringVar(&httpPort, "http", "9000", "the http port to listen on") |
|
} |
|
|
|
func handleByCode(w http.ResponseWriter, r *http.Request) { |
|
o := strings.Split(r.URL.Path, "/") |
|
code := 503 |
|
if len(o) >= 3 { |
|
c, err := strconv.Atoi(o[2]) |
|
if err == nil { |
|
code = c |
|
} |
|
} |
|
fmt.Printf("handleByCode: %v\n", code) |
|
w.WriteHeader(code) |
|
} |
|
|
|
func handleHTTP(w http.ResponseWriter, r *http.Request) { |
|
fmt.Printf("[%v] request path: %v\n", time.Now(), r.URL.Path) |
|
if strings.HasPrefix(r.URL.Path, "/code/") { |
|
handleByCode(w, r) |
|
return |
|
} |
|
} |
|
|
|
func handleTCP(conn net.Conn) { |
|
// Make a buffer to hold incoming data. |
|
buf := make([]byte, 1024) |
|
// Read the incoming connection into the buffer. |
|
len, err := conn.Read(buf) |
|
if err != nil { |
|
fmt.Println("Error reading:", err.Error()) |
|
} |
|
payload := string(buf[:len]) |
|
fmt.Printf("received tcp conn\nremote address %v\nlocal address: %v\ndata\n%v", |
|
conn.RemoteAddr(), conn.LocalAddr(), string(payload)) |
|
lines := strings.Split(payload, "\n") |
|
cmd := "reset" |
|
for _, i := range lines { |
|
if strings.Contains(i, "cmd") { |
|
cmd = strings.TrimSpace(strings.Split(i, " ")[1]) |
|
break |
|
} |
|
} |
|
fmt.Printf("execute command:%v\n", cmd) |
|
switch cmd { |
|
case "hang": |
|
time.Sleep(3600 * time.Second) |
|
break |
|
default: |
|
conn.Close() |
|
} |
|
} |
|
|
|
func main() { |
|
flag.Parse() |
|
fmt.Printf("hello world, starting the server at port %v\n", httpPort) |
|
http.HandleFunc("/", handleHTTP) |
|
if err := http.ListenAndServe(fmt.Sprintf("localhost:%v", httpPort), nil); err != nil { |
|
panic(fmt.Sprintf("failed to listen http server: %v", err)) |
|
} |
|
} |