'Golang run on Windows without deal with the Firewall
I'm working on a Rest API with Go, but everytime I try to run my application with
go run main.go
the Windows Firewall tells me that has blocked some features of my app. I would like to know if there's some way to make my executions without have to Accept everytime.
Solution 1:[1]
Hi I had the same problem: Try that:
- Go to Windows Defender Firewall, in Left side menu you saw Inbound Rules click there, then Right Side menu you will see New Rule... click.
- Choose Port opened from window -> Next Select TCP, then define which ports you want I choose 8080 click Next again, Choose Allow the connection Next, Check All Next, Give any Name Goland or anything you want and press Finish. Thats it
Solution 2:[2]
Change
http.ListenAndServe(":3000", r)
to
http.ListenAndServe("127.0.0.1:3000", r)
Solution 3:[3]
If you are calling go run main.go
following is happening:
- your programm is compiled inside a temporary folder
- the compiled binary is executed
But the temporary folder is just for one execution. So the next time when you run your programm via go run
another folder is used.
The windows firewall does give you always the information which path your server has and if you remember the paths after each time you will see that there is always a different path.
The windows firewall is so configuread that it remembers the path of each programm. So when the path is changing you will always need to comfirm that the new path is allowed to run on that port.
To fix this you should compile your server. Just run go build
and exeute the binaries then inside you project folder. Then you will just have to accept just one time.
Solution 4:[4]
based on @apxp answer
in windows cli this works for me
go build main.go && main.exe
Solution 5:[5]
this work for me
go build main.go && .\main.exe
and run using makefile
Solution 6:[6]
I think @apxp's answer is the complete explanation of the situation; some time after ask this question I found a way to run my application with:
go build -o ejecutable.exe ; if($?) { .\ejecutable.exe }
Solution 7:[7]
Just go to your Windows Firewall notification settings:
Control Panel -> Windows Defender Firewall -> Change notification settings
Uncheck the option for Notify me when Windows Defender Firewall blocks a new app to prevent it from showing the popup.
Solution 8:[8]
You can use CompileDaemon to auto re-build your project on file changes, because it runs a build anyway, you'll only have to accept once. Plus, your project will re-build automatically!
Install:
go get https://github.com/githubnemo/CompileDaemon
Example usage:
# Assuming your project looks like and you're in your project working dir
# hello/
# hello.go
# Rebuild on .go file edits and run the hello command after
CompileDaemon -command="./hello"
Solution 9:[9]
The WSL run under VM, so you have to execute ifconfig
You will see your IP in the section (eth0:) inet x.x.x.x
This x.x.x.x is the IP you have to put in your browser.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow