Connect reset by peer with Golang http server on Mac OS10.9.5 - macos

I got an weird error when writing some stress tests on a golang web application.
Here is the code for web server
package httptest
import (
"net/http"
"testing"
)
func TestHttpTest(t *testing.T) {
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
//<-time.After(2 * time.Second)
})
http.ListenAndServe(":2222", nil)
}
When I run go test ./... and use ApacheBench to perform stress test:
ab -c 200 -n 1000 http://localhost:2222/123
It gives me following error randomly:
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
apr_socket_recv: Connection reset by peer (54)
Total of 235 requests completed
I am running the test on Mac OS 10.9. The ulimit of the os is setting as unlimited. Does anyone meet the same problem?

Related

What causes urn:acme:error:unauthorized 403 error in golang's acme/autocert?

The full error message is:
403 urn:acme:error:unauthorized: Account creation on ACMEv1 is
disabled. Please upgrade your ACME client to a version that supports
ACMEv2 / RFC 8555. See
https://community.letsencrypt.org/t/end-of-life-plan-for-acmev1/88430
for details
And I've googled this and reviewed that link, but I'm just using:
golang.org/x/crypto/acme/autocert
package in a very normal way:
package main
import (
"crypto/tls"
"net/http"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/acme/autocert"
)
func main() {
router := gin.Default()
hosts := []string{"yourdomain.com"}
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(hosts...),
Cache: autocert.DirCache("/certs"),
}
server := &http.Server{
Addr: ":https",
Handler: router,
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
},
}
server.ListenAndServeTLS("", "")
}
In fact this code has been running and working fine for the last 6 months. But just today I switched the server it was on and now get the above message.
I tried getting the very latest version of golang, but still same problem.
I changed my DNS for my hosts to this new server's ip and the hostname of the server is correct.
Far as I can tell, it's 100% identical to the previous working server but with a new IP.
Is golang's acme/autocert really this out of date and not using ACMEv2?
This statement:
In fact this code has been running and working fine for the last 6 months. But just today I switched the server it was on and now get the above message.
Might indicate that you're building against an older version of golang.org/x/crypto - check your go.mod file and ensure you're using a fairly recent version. I completed a project recently that uses almost identical code. The require in my go.mod looks like this:
golang.org/x/crypto v0.0.0-20200602180216-279210d13fed

Unable to create online web-page

I am trying to create Golang web-pages...
Progress:
Ubuntu 18.04 installed both locally and on a Linode VPS.
Created and compiled a local Golang "Hello World" script that renders OK both locally and online.
Created a net/http Golang script that works OK when called locally http://localhost:8080/testing to see if it works
Uploaded the script to the Linode server and initial status messages appear but when calling http:123.456.789.32:8080/testing to see if it works the browser freezes.
//
// Golang - main.go
//
package main
import (
"net/http"
)
func sayHello(w http.ResponseWriter, r *http.Request) {
message := r.URL.Path
message = "Hello " + message
w.Write([]byte(message))
}
func main() {
http.HandleFunc("/", sayHello)
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
There are no errors or warnings rendered and unable to find any log references.
Can error and warnings similar to PHP error_reporting(-1), declare(strict_types=1) etc be logged or rendered?
A quick check with Nmap showed this result:
nmap -sV -p 8080 <yourIP>
Starting Nmap 7.70 ( https://nmap.org ) at 2019-07-04 07:45 CEST
Nmap scan report for <your-domain>.com (<yourIP>)
Host is up (0.032s latency).
PORT STATE SERVICE VERSION
8080/tcp filtered http-proxy
Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 0.90 seconds
The state of "filtered" actually means that there was no response on that port as opposed to an outright rejection of the request.
Check the output of iptables -L -n. Presumably, you have a firewall running and blocking port 8080. Do not simply deactivate the firewall, but read up on how to open port 8080 in the firewall product you are using. Linode has guides for the commonly used/preinstalled firewalls of various Linux distributions.
If you plan to go into production, please have someone help you to ensure security and availability of your deployment.

HTTPS request restarts NodeMCU

NodeMCU Info:
> NodeMCU custom build by frightanic.com
> branch: dev
> SSL: true
> modules: crypto,file,gpio,http,net,node,sjson,tmr,uart,wifi,tls
> powered by Lua 5.1.4 on SDK 2.2.1
> commit: d5aca39f485bf46e6d77b7fb05eaf71b8ea51bee
> Build type: integer -- tried float, same error
> LFS : disabled
Build bin file
When I requested HTTPS request before adding the tls module it returned code -1 after I added tls module requesting HTTPS restarts the NodeMCU.
The code I used to send request.
print(node.heap()) -- 31824
http.request("https://www.google.com", "HEAD", "", "",
function(code, data)
if (code < 0) then
print("HTTP request failed")
else
print(code, data)
end
end)
and I tried the net module.
print(node.heap()) -- 31824
srv = tls.createConnection()
srv:on("receive", function(sck, c) print(c) end)
srv:on("connection", function(sck, c)
sck:send("GET / HTTP/1.1\r\nHost: google.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
end)
srv:connect(443,"google.com")
I got the same restart reason for both codes.
HTTP client: hostname=google.com
HTTP client: port=443
HTTP client: method=GET
HTTP client: path=/
HTTP client: DNS request
HTTP client: DNS pending
> HTTP client: DNS found google.com 172.217.18.46
client handshake start.
Fatal exception 0(IllegalInstructionCause):
epc1=0x4027ca20, epc2=0x00000000, epc3=0x00000000, excvaddr=0x402993e7, depc=0x00000000
รพ
ets Jan 8 2013,rst cause:2, boot mode:(3,x) -- sometimes x is 0, 6 or 7
load 0x40100000, len 26104, room 16
tail 8
chksum 0xc0
load 0x3ffe8000, len 2332, room 0
tail 12
chksum 0x3c
ho 0 tail 12 room 4
load 0x3ffe891c, len 136, room 12
tail 12
chksum 0x56
csum 0x56
in the net moudle code the restart happens when srv:connect(443,"google.com") is called and shows one additional line RecvHold, By pespconn,find conn_msg fail
Assuming the date in your error is because of something you executed recently, I suggest that you set the time to a more recent one. If your time has drifted too much your OS cannot operate normally. Set time manuelly if NTP refuse your connection. How to set RTC rtctime module, but also look at sntp module or other relevant ones.
I overcome this problem by building the same SDK Firmware 2.2.1 with Docker with the same modules and same configuration, can't control cases and lines to debug in the Cloud Build version so cant know what exactly went wrong in the Cloud Build.
The Docker Firmware info
NodeMCU 2.2.0.0 built with Docker provided by frightanic.com
branch: master
commit: 4095c408e6a8cc9718cb06007b408d0aad15d9cd
SSL: true
Build type: integer
LFS: disabled
modules: crypto,file,gpio,http,net,node,sjson,tls,tmr,uart,wifi
build created on 2018-11-29 17:45
powered by Lua 5.1.4 on SDK 2.2.1(6ab97e9)

http.ListenAndServe handler function executed twice on port 80 [duplicate]

This question already has answers here:
HandleFunc being called twice
(4 answers)
Why this simple web server is called even number times?
(1 answer)
Closed 5 years ago.
If I run the following simple http server code on port 8080 everything works as expected. If I run the same code on port 80, by just changing the port, the handler function is executed twice with each request. Why, and how to fix it?
// httptest project main.go
package main
import (
"net/http"
"log"
"fmt"
"html"
)
var count int
func defaultHandler(w http.ResponseWriter, r *http.Request) {
count++
fmt.Fprintf(w, "Hello, %q count=%d", html.EscapeString(r.URL.Path), count)
fmt.Println(count,r.RemoteAddr)
}
func main() {
http.HandleFunc("/", defaultHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
If I enter localhost:8080 in the browser, I get a response with a count starting at 1 and increased by 1 with each new request.
If I change the code to port 80 and enter just localhost or localhost:80 in the browser, I get a first response with a count starting at 1 but increased by two with each following request. At the same time the print statement for the console output is executed twice.
Terminal console when running on port 80 with 3 requests:
>go run main.go
1 [::1]:51335
2 [::1]:51335
3 [::1]:51335
4 [::1]:51335
5 [::1]:51335
6 [::1]:51335
The responses in the browser are Hello, "/" count=1, Hello, "/" count=3 and Hello, "/" count=5.
I've been running this locally on Windows 10 with Go version go1.9.2 windows/amd64 and the latest Google Chrome Browser.
However, I detected the issue in a simple web application on a remote Linux server where the code has been compiled with go version go1.9.1 linux/amd64.
i just tried it on my pc with Fiddler open
I noticed when navigating to the url using Google Chrome, the browser makes 2 request
GET / HTTP/1.1
GET /favicon.ico HTTP/1.1
the request for the favicon also gets handled by the defaultHandler, which causes the count to increment
I also tried with firefox and it doesn't send another request for the favicon
Try to log requests. Possibly browser is calling /favicon.ico

Get golang http server working with rerun

I'm trying to use rerun to relaunch a go http server when the source files change, but the restart always fails to launch.
Simple server
package main
import (
"net/http"
"fmt"
"log"
"html"
)
func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
Command line output
$ rerun -p "**/*.{go,html}" go run my_server.go
16:49:24 [rerun] Rerun_test launched
16:49:26 [rerun] Watching . for **/*.{go,html} using Darwin adapter
16:50:17 [rerun] Change detected: 1 modified
16:50:17 [rerun] Sending signal TERM to 75688
16:50:17 [rerun] Rerun_test restarted
2014/07/15 16:50:17 listen tcp :8080: bind: address already in use
exit status 1
16:50:19 [rerun] Rerun_test Launch Failed
How can I get this working, or why can't the server bind to the port when it is relaunched?
Also, I am using OSX 10.9.
A process already running on port 8080 that's why it cannot re-run, go to your activity monitor find the process in your case it may be named as (my_server), and quit it.

Resources