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

add bindAddr for darwin #2609

Merged
merged 1 commit into from
Oct 16, 2023
Merged

add bindAddr for darwin #2609

merged 1 commit into from
Oct 16, 2023

Conversation

hossinasaadi
Copy link
Contributor

No description provided.

@yuhan6665
Copy link
Member

Thank a lot for your pr! Though I have a question.. how do you use this bind address in sockopt?

@us254
Copy link

us254 commented Oct 9, 2023

The bind address is not directly used in the socket options (sockopt). Instead, it's used in the bindAddr function to bind the socket to a specific IP address and port. After this binding, the address is implicitly utilized in subsequent system calls for network communication. The socket options SO_REUSEADDR and SO_REUSEPORT are set separately using the unix.SetsockoptInt function to allow the reusing of local addresses and ports.

@yuhan6665
Copy link
Member

@us254 right, but how to activate this feature? I see a code block commented out by @RPRX some time ago..
c41a1a5

@us254
Copy link

us254 commented Oct 11, 2023

// Assuming fd, address, and port are appropriately defined
// Set up the socket configurations
sockopt := &internet.SocketConfig{
	Tproxy: internet.SocketConfig_TProxy,
}

if dest.Address.Family().IsIP() {
	sockopt.BindAddress = dest.Address.IP()
	sockopt.BindPort = uint32(dest.Port)
}

if d.sockopt != nil {
	sockopt.Mark = d.sockopt.Mark
}

// Call the functions to bind address and set socket options
err := bindAddr(fd, sockopt.BindAddress, sockopt.BindPort)
if err != nil {
    return err
}

err = setReuseAddr(fd)
if err != nil {
    return err
}

err = setReusePort(fd)
if err != nil {
    return err
}

// Dial a system-level connection
tConn, err := internet.DialSystem(ctx, net.DestinationFromAddr(conn.RemoteAddr()), sockopt)
if err != nil {
	return err
}
defer tConn.Close()

writer = &buf.SequentialWriter{Writer: tConn}
tReader := buf.NewPacketReader(tConn)
requestCount++
tproxyRequest = func() error {
	defer func() {
		if atomic.AddInt32(&requestCount, -1) == 0 {
			timer.SetTimeout(plcy.Timeouts.DownlinkOnly)
		}
	}()
	if err := buf.Copy(tReader, link.Writer, buf.UpdateActivity(timer)); err != nil {
		return newError("failed to transport request (TPROXY conn)").Base(err)
	}
	return nil
}

}
}

@hossinasaadi
Copy link
Contributor Author

@yuhan6665 , sorry for late reply, I have worked on a customized version of Xray for macOS and realized that the reuse port and reuse address did not work in Darwin, so I feel it may be helpful for the main repo.
it can be used kinda like @us254 mentioned before.

@yuhan6665 yuhan6665 merged commit 6177ec7 into XTLS:main Oct 16, 2023
35 checks passed
@yuhan6665
Copy link
Member

Ok let's merge this for now. Thanks all!

@hossinasaadi hossinasaadi deleted the bindAddrDarwin branch October 16, 2023 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants