45

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.

hestellezg
  • 3,309
  • 3
  • 33
  • 37
  • 24
    If by any chance the action we're talking about is starting an HTTP(s) server, then while you develop, use localhost, e.g. instead of `":8080"`, use `"localhost:8080"`. That is not a restricted action. – icza Mar 16 '19 at 22:02

9 Answers9

78

Change

http.ListenAndServe(":3000", r)

to

http.ListenAndServe("127.0.0.1:3000", r)
jeffasante
  • 2,549
  • 2
  • 8
  • 8
  • 7
    `http.ListenAndServe("localhost:3000", r)` worked for me too – TKharaishvili May 04 '21 at 22:27
  • 7
    don't forget to set it back to http.ListenAndServe(":3000", r) when you deploy it to your production server guys. except you use reverse proxy for it, you would not be able to access it remotely. got me a headache for few hours before I realize that this is the cause :) – Jacky Supit Aug 20 '21 at 07:11
  • This works, but if you’re on windows, I’d recommend the solution from Ismayil - https://stackoverflow.com/a/65393403/9808334 – LudacrisX1 Jul 14 '22 at 16:23
  • @JackySupit Thank you for your important hint. You could also give this information via a command-line flag: go run ./cmd/web -addr="localhost:9090" – Twistleton Sep 28 '22 at 14:59
22

If you are calling go run main.go the following is happening:

  • your program 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 program via go run another folder is used.

The Windows firewall always gives you the 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 configured so that it remembers the path of each program. So when the path is changed you will always need to confirm that the new path is allowed to run on that port.

To fix this you should compile your server. Just run go build and execute the binaries inside your project folder. Then you will just have to accept once.

James Skemp
  • 8,018
  • 9
  • 64
  • 107
apxp
  • 5,240
  • 4
  • 23
  • 43
  • 2
    this is a good explanation of why this is actually happening but I think @the Mage has a better solution of fixing the issue aside from just build the the program. – masonCherry Jul 22 '21 at 13:19
18

Hi I had the same problem: Try that:

  1. 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.
  2. 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
2

this work for me

go build main.go && .\main.exe

and run using makefile

barungerti441
  • 113
  • 1
  • 2
  • 5
1

based on @apxp answer

in windows cli this works for me

go build main.go && main.exe

Subramanya Rao
  • 151
  • 3
  • 6
0

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 }
hestellezg
  • 3,309
  • 3
  • 33
  • 37
0

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.

Emre
  • 1,239
  • 9
  • 7
0

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"
Tom Mertz
  • 626
  • 9
  • 14
0

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.

Mike Brian Olivera
  • 1,414
  • 1
  • 16
  • 21