Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hostname listen. #2

Merged
merged 2 commits into from
Mar 23, 2024
Merged
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
13 changes: 12 additions & 1 deletion .github/workflows/test-startup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,18 @@ jobs:
sudo systemctl daemon-reload

- name: Start go-mmproxy
run: sudo systemctl start go-mmproxy || sudo systemctl status go-mmproxy.service
run: sudo systemctl start go-mmproxy || sudo journalctl -xeu go-mmproxy.service

- name: Check go-mmproxy status
run: systemctl is-active go-mmproxy

- name: Replace listen address of go-mmproxy with a hostname
run: |
sudo sed -i 's/-l 0.0.0.0:1234/-l %H:1234/' /etc/systemd/system/go-mmproxy.service
sudo systemctl daemon-reload

- name: Restart go-mmproxy
run: sudo systemctl restart go-mmproxy || sudo journalctl -xeu go-mmproxy.service

- name: Check go-mmproxy status
run: systemctl is-active go-mmproxy
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func main() {
}

var err error
if Opts.ListenAddr, err = netip.ParseAddrPort(Opts.ListenAddrStr); err != nil {
if Opts.ListenAddr, err = ParseHostPort(Opts.ListenAddrStr); err != nil {
Opts.Logger.Error("listen address is malformed", "error", err)
os.Exit(1)
}
Expand Down
25 changes: 25 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package main
import (
"fmt"
"net"
"net/netip"
"strconv"
"syscall"
)

Expand All @@ -30,6 +32,29 @@ func CheckOriginAllowed(remoteIP net.IP) bool {
return false
}

func ParseHostPort(hostport string) (netip.AddrPort, error) {
host, portStr, err := net.SplitHostPort(hostport)
if err != nil {
return netip.AddrPort{}, fmt.Errorf("failed to parse host and port: %w", err)
}

ips, err := net.LookupIP(host)
if err != nil {
return netip.AddrPort{}, fmt.Errorf("failed to lookup IP addresses: %w", err)
}
if len(ips) == 0 {
return netip.AddrPort{}, fmt.Errorf("no IP addresses found")
}

port, err := strconv.ParseUint(portStr, 10, 16)
if err != nil {
return netip.AddrPort{}, fmt.Errorf("failed to parse port: %w", err)
}

ip, _ := netip.AddrFromSlice(ips[0])
return netip.AddrPortFrom(ip, uint16(port)), nil
}

func DialUpstreamControl(sport int) func(string, string, syscall.RawConn) error {
return func(network, address string, c syscall.RawConn) error {
var syscallErr error
Expand Down
Loading