Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ information about HTTP request headers and bodies back to the client.

The `PORT` environment variable sets the server port, which defaults to `8080`.

### SEND_SERVER_REMOTE

The `SEND_SERVER_REMOTE` variable set remote address display or

```bash
curl -H '' -H 'X-Send-Server-Remote: true' http://dnsname.domain
```

Add RemoteAddr, RemoteIP, RemotePort to echo response

```
RemoteAddr: xx:yy
RemoteIP: xx
RemotePort: yy
```


### Logging

Set the `LOG_HTTP_HEADERS` environment variable to print request headers to
Expand Down Expand Up @@ -62,6 +79,23 @@ go get -u github.com/jmalloc/echo-server/...
PORT=10000 echo-server
```

```
export GOPROXY=https://goproxy.cn,direct && SEND_SERVER_REMOTE=false PORT=10001 go run cmd/echo-server/main.go
```

## build and package

```bash
go build -o artifacts/echo-server ./cmd/echo-server
# build
GOOS=linux GOARCH=amd64 go build -o artifacts/echo-server_linux_amd64 ./cmd/echo-server
GOOS=linux GOARCH=arm64 go build -o artifacts/echo-server_linux_arm64 ./cmd/echo-server
GOOS=darwin GOARCH=amd64 go build -o artifacts/echo-server_mac_x86_64 ./cmd/echo-server
GOOS=darwin GOARCH=arm64 go build -o artifacts/echo-server_mac_arm64 ./cmd/echo-server
GOOS=windows GOARCH=amd64 go build -o artifacts/echo-server_windows_amd64 ./cmd/echo-server
ls -lah artifacts/*
```

### Running under Docker

To run the latest version as a container:
Expand Down
66 changes: 66 additions & 0 deletions cmd/echo-server/html/frontend.tmpl.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<html>
<head>
<title>websocket</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<style>
#console {
Expand Down Expand Up @@ -52,6 +53,9 @@
border-radius: 0.3em;
border: 1px solid lightgray;
background: white;
padding: 0.5em 1em;
font-size: 14px;
margin: 0.25em;
}

#msg {
Expand All @@ -67,6 +71,7 @@
display: block;
min-height: 8em;
min-width: 20em;
font-size: 14px;
}

#msg button {
Expand All @@ -82,6 +87,67 @@
border-radius: 0.3em;
background: white;
padding: 0.5em;
max-width: 300px;
z-index: 1000;
}

@media (max-width: 768px) {
#panel {
position: fixed;
top: auto;
bottom: 0;
left: 0;
right: 0;
max-width: 100%;
border-radius: 0;
border-top: 2px solid lightgray;
padding: 1em;
background: white;
box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
}

#panel > div {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 0.5em;
}

button {
flex: 1;
min-width: 120px;
font-size: 16px;
padding: 0.75em 1em;
}

#msg textarea {
min-height: 6em;
font-size: 16px;
}

#console {
padding-bottom: 200px;
}

#console div:before {
width: 4em;
}
}

@media (max-width: 480px) {
button {
min-width: 100px;
font-size: 14px;
padding: 0.5em 0.75em;
}

#msg textarea {
min-height: 5em;
}

#console {
padding-bottom: 180px;
}
}

</style>
Expand Down
45 changes: 36 additions & 9 deletions cmd/echo-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ func handler(wr http.ResponseWriter, req *http.Request) {
"false",
)

sendServerRemoteString := os.Getenv("SEND_SERVER_REMOTE")
if v := req.Header.Get("X-Send-Server-Remote"); v != "" {
sendServerRemoteString = v
}

sendServerRemote := strings.EqualFold(
sendServerRemoteString,
"true",
)

for _, line := range os.Environ() {
parts := strings.SplitN(line, "=", 2)
key, value := parts[0], parts[1]
Expand All @@ -102,17 +112,17 @@ func handler(wr http.ResponseWriter, req *http.Request) {
}

if websocket.IsWebSocketUpgrade(req) {
serveWebSocket(wr, req, sendServerHostname)
serveWebSocket(wr, req, sendServerHostname, sendServerRemote)
} else if path.Base(req.URL.Path) == ".ws" {
serveFrontend(wr, req)
} else if path.Base(req.URL.Path) == ".sse" {
serveSSE(wr, req, sendServerHostname)
serveSSE(wr, req, sendServerHostname, sendServerRemote)
} else {
serveHTTP(wr, req, sendServerHostname)
serveHTTP(wr, req, sendServerHostname, sendServerRemote)
}
}

func serveWebSocket(wr http.ResponseWriter, req *http.Request, sendServerHostname bool) {
func serveWebSocket(wr http.ResponseWriter, req *http.Request, sendServerHostname bool, sendServerRemote bool) {
connection, err := upgrader.Upgrade(wr, req, nil)
if err != nil {
fmt.Printf("%s | %s\n", req.RemoteAddr, err)
Expand Down Expand Up @@ -188,7 +198,7 @@ func serveFrontend(wr http.ResponseWriter, req *http.Request) {
wr.WriteHeader(200)
}

func serveHTTP(wr http.ResponseWriter, req *http.Request, sendServerHostname bool) {
func serveHTTP(wr http.ResponseWriter, req *http.Request, sendServerHostname bool, sendServerRemote bool) {
wr.Header().Add("Content-Type", "text/plain")
wr.WriteHeader(200)

Expand All @@ -201,17 +211,17 @@ func serveHTTP(wr http.ResponseWriter, req *http.Request, sendServerHostname boo
}
}

writeRequest(wr, req)
writeRequest(wr, req, sendServerRemote)
}

func serveSSE(wr http.ResponseWriter, req *http.Request, sendServerHostname bool) {
func serveSSE(wr http.ResponseWriter, req *http.Request, sendServerHostname bool, sendServerRemote bool) {
if _, ok := wr.(http.Flusher); !ok {
http.Error(wr, "Streaming unsupported!", http.StatusInternalServerError)
return
}

var echo strings.Builder
writeRequest(&echo, req)
writeRequest(&echo, req, sendServerRemote)

wr.Header().Set("Content-Type", "text/event-stream")
wr.Header().Set("Cache-Control", "no-cache")
Expand Down Expand Up @@ -290,11 +300,28 @@ func writeSSEField(
}

// writeRequest writes request headers to w.
func writeRequest(w io.Writer, req *http.Request) {
func writeRequest(w io.Writer, req *http.Request, sendServerRemote bool) {
fmt.Fprintf(w, "%s %s %s\n", req.Method, req.URL, req.Proto)
fmt.Fprintln(w, "")

fmt.Fprintf(w, "Host: %s\n", req.Host)

if sendServerRemote {
fmt.Fprintf(w, "RemoteAddr: %s\n", req.RemoteAddr)

// 解析IP地址和端口
ip := req.RemoteAddr
port := ""
if idx := strings.LastIndex(req.RemoteAddr, ":"); idx != -1 {
ip = req.RemoteAddr[:idx]
port = req.RemoteAddr[idx+1:]
// 移除IPv6地址的方括号
ip = strings.TrimPrefix(strings.TrimSuffix(ip, "]"), "[")
}
fmt.Fprintf(w, "RemoteIP: %s\n", ip)
fmt.Fprintf(w, "RemotePort: %s\n", port)
}

printHeaders(w, req.Header)

var body bytes.Buffer
Expand Down