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

feat: support mieru protocol (issue #1563) #1574

Closed
wants to merge 1 commit into from

Conversation

enfein
Copy link

@enfein enfein commented Oct 8, 2024

Add mieru outbound proxy.

With the existing public APIs defined in mieru, it can only handle TCP connections. UDP may be supported in the future.

Tested both mieru TCP and UDP transport, and they were working.

$ cat config.yaml 
mixed-port: 7890
mode: rule
proxies:
  - name: mieru-tcp
    type: mieru
    server: a.b.c.d
    port: 2999
    transport: TCP
    username: abc
    password: abc
  - name: mieru-udp
    type: mieru
    server: a.b.c.d
    port-range: 2999-3000
    transport: UDP
    username: abc
    password: abc
    mtu: 1400
rules:
  - MATCH,mieru-tcp
  # - MATCH,mieru-udp
$ ./mihomo-linux-amd64-compatible  
INFO[...] [TCP] 127.0.0.1:43592 --> ww.xx.yy.zz:80 match Match using mieru-tcp
$ curl --socks5 127.0.0.1:7890 http://example.com
<!doctype html>
<html>
<head>
    <title>Example Domain</title>

    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style type="text/css">
    body {
        background-color: #f0f0f2;
        margin: 0;
        padding: 0;
        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        
    }
    div {
        width: 600px;
        margin: 5em auto;
        padding: 2em;
        background-color: #fdfdff;
        border-radius: 0.5em;
        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
    }
    a:link, a:visited {
        color: #38488f;
        text-decoration: none;
    }
    @media (max-width: 700px) {
        div {
            margin: 0 auto;
            width: auto;
        }
    }
    </style>    
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

@xishang0128 xishang0128 force-pushed the Alpha branch 2 times, most recently from 22addc6 to 5772507 Compare October 10, 2024 23:36
@wwqgtxx
Copy link
Collaborator

wwqgtxx commented Oct 11, 2024

Thank you for your contribution. After reading the PR code:

We need to use mihomo's internal dialer to initiate network connections, avoid loopback problems in tun and other modes, and support the chain proxy function of dialer-proxy.

I found that your mieru code uses a lot of low-level *net.TCPConn and *net.UDPConn instances instead of the more general net.Conn and net.PacketConn interfaces, which also prevents the use of mihomo's internal dialer, which does not guarantee the return of these two specific types of instances.

In addition, it should be noted that all dns resolutions occurring on the client should also go through the internal resolver instead of net.DefaultResolver, including direct and indirect calls (such as using net.ResolveUDPAddr and net.ResolveTCPAddr to resolve non-pure IP targets).

@enfein
Copy link
Author

enfein commented Oct 11, 2024

@wwqgtxx

Thank you for reviewing.

There are several features needed in mieru:

  1. Support dialer proxy to connect to proxy server.
  2. Avoid concrete *net.TCPConn and *net.UDPConn types in the implementation, because the actual connection type can be different.
  3. Provide an API to set the DNS resolver in mieru.

After completing those, I will send the PR again.

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.

2 participants