GithubHelp home page GithubHelp logo

Comments (28)

grayscott avatar grayscott commented on August 11, 2024

func (r *Reply) WriteTo(w io.Writer) (int64, error) {
var n int
i, err := w.Write([]byte{r.Ver, r.Rep, r.Rsv, r.Atyp})
n = n + i
if err != nil {
return int64(n), err
}
i, err = w.Write(r.BndAddr)
n = n + i
if err != nil {
return int64(n), err
}
i, err = w.Write(r.BndPort)
n = n + i
if err != nil {
return int64(n), err
}
if Debug {
log.Printf("Sent Reply: %#v %#v %#v %#v %#v %#v\n", r.Ver, r.Rep, r.Rsv, r.Atyp, r.BndAddr, r.BndPort)
}
return int64(n), nil
}

from socks5.

txthinking avatar txthinking commented on August 11, 2024

What is the 'problem'?

from socks5.

grayscott avatar grayscott commented on August 11, 2024

you write associate ack data in three time, client will read loss data

from socks5.

grayscott avatar grayscott commented on August 11, 2024

func (r *Reply) WriteTo(w io.Writer) (int64, error) {
var n int
buffer := bytes.NewBuffer([]byte{r.Ver, r.Rep, r.Rsv, r.Atyp})
buffer.Write(r.BndAddr)
buffer.Write(r.BndPort)
i, err := w.Write(buffer.Bytes())
n = n + i
if err != nil {
return int64(n), err
}
fmt.Printf("debug value:%t", Debug)
if Debug {
log.Printf("Sent Reply: %#v %#v %#v %#v %#v %#v\n", r.Ver, r.Rep, r.Rsv, r.Atyp, r.BndAddr, r.BndPort)
}
return int64(n), nil
}

from socks5.

grayscott avatar grayscott commented on August 11, 2024

this is change function

from socks5.

txthinking avatar txthinking commented on August 11, 2024

TCP is a stream protocol, so write([0x01]), write([0x02]) is same with write([0x01, 0x02])

from socks5.

grayscott avatar grayscott commented on August 11, 2024

but the result is not that on my pc and if you socket set so_nodelay flag the result is separate

from socks5.

txthinking avatar txthinking commented on August 11, 2024

from socks5.

txthinking avatar txthinking commented on August 11, 2024

from socks5.

txthinking avatar txthinking commented on August 11, 2024

from socks5.

grayscott avatar grayscott commented on August 11, 2024

image
this is the tcpconnect description

from socks5.

grayscott avatar grayscott commented on August 11, 2024

so that you three times to write socket will send three packages in network

from socks5.

grayscott avatar grayscott commented on August 11, 2024

could you give me privilege to push the code, there has two to fault with udp proxy

from socks5.

txthinking avatar txthinking commented on August 11, 2024

Can you copy this code and run, then paste the output:

package main

import (
	"io"
	"log"
	"net"
	"time"
)

func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)

	a, err := net.ResolveTCPAddr("tcp", "127.0.0.1:9999")
	if err != nil {
		log.Println(err)
		return
	}
	go func() {
		s, err := net.ListenTCP("tcp", a)
		if err != nil {
			log.Println(err)
			return
		}
		for {
			conn, err := s.AcceptTCP()
			if err != nil {
				log.Println(err)
				return
			}
			log.Println("server set no delay", conn.SetNoDelay(true))
			n, err := conn.Write([]byte{0x01})
			if err != nil {
				log.Println(err)
				return
			}
			log.Println("server write to client length", n, "data", []byte{0x01})
			n, err = conn.Write([]byte{0x02})
			if err != nil {
				log.Println(err)
				return
			}
			log.Println("server write to client length", n, "data", []byte{0x02})
			conn.Close()
		}
	}()

	time.Sleep(3 * time.Second)

	conn, err := net.DialTCP("tcp", nil, a)
	if err != nil {
		log.Println(err)
		return
	}
	log.Println("client set no delay", conn.SetNoDelay(true))
	b := make([]byte, 2)
	n, err := io.ReadFull(conn, b)
	if err != nil {
		log.Println(err)
		return
	}
	log.Println("client read from server length", n, "data", b)

	time.Sleep(3 * time.Second)
}

Here is output I got:

2020/05/25 20:09:28 a.go:54: client set no delay <nil>
2020/05/25 20:09:28 a.go:30: server set no delay <nil>
2020/05/25 20:09:28 a.go:36: server write to client length 1 data [1]
2020/05/25 20:09:28 a.go:42: server write to client length 1 data [2]
2020/05/25 20:09:28 a.go:61: client read from server length 2 data [1 2]

from socks5.

txthinking avatar txthinking commented on August 11, 2024

Yes you and anyone can send PR, don't need my authorization

from socks5.

grayscott avatar grayscott commented on August 11, 2024

ok

from socks5.

txthinking avatar txthinking commented on August 11, 2024

Can you copy this code and run, then paste the output:

package main

import (
	"io"
	"log"
	"net"
	"time"
)

func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)

	a, err := net.ResolveTCPAddr("tcp", "127.0.0.1:9999")
	if err != nil {
		log.Println(err)
		return
	}
	go func() {
		s, err := net.ListenTCP("tcp", a)
		if err != nil {
			log.Println(err)
			return
		}
		for {
			conn, err := s.AcceptTCP()
			if err != nil {
				log.Println(err)
				return
			}
			log.Println("server set no delay", conn.SetNoDelay(true))
			n, err := conn.Write([]byte{0x01})
			if err != nil {
				log.Println(err)
				return
			}
			log.Println("server write to client length", n, "data", []byte{0x01})
			n, err = conn.Write([]byte{0x02})
			if err != nil {
				log.Println(err)
				return
			}
			log.Println("server write to client length", n, "data", []byte{0x02})
			conn.Close()
		}
	}()

	time.Sleep(3 * time.Second)

	conn, err := net.DialTCP("tcp", nil, a)
	if err != nil {
		log.Println(err)
		return
	}
	log.Println("client set no delay", conn.SetNoDelay(true))
	b := make([]byte, 2)
	n, err := io.ReadFull(conn, b)
	if err != nil {
		log.Println(err)
		return
	}
	log.Println("client read from server length", n, "data", b)

	time.Sleep(3 * time.Second)
}

Here is output I got:

2020/05/25 20:09:28 a.go:54: client set no delay <nil>
2020/05/25 20:09:28 a.go:30: server set no delay <nil>
2020/05/25 20:09:28 a.go:36: server write to client length 1 data [1]
2020/05/25 20:09:28 a.go:42: server write to client length 1 data [2]
2020/05/25 20:09:28 a.go:61: client read from server length 2 data [1 2]

This is minimal tcp read write, about your question

from socks5.

grayscott avatar grayscott commented on August 11, 2024

image

from socks5.

grayscott avatar grayscott commented on August 11, 2024

but your read is block read full two bytes

from socks5.

txthinking avatar txthinking commented on August 11, 2024

Yes, this is what func NewReplyFrom(r io.Reader) (*Reply, error) { does

from socks5.

grayscott avatar grayscott commented on August 11, 2024

git.exe push --progress "origin" master:master
remote: Permission to txthinking/socks5.git denied to grayscott.

from socks5.

grayscott avatar grayscott commented on August 11, 2024

I can't push my code

from socks5.

grayscott avatar grayscott commented on August 11, 2024

my socks5 client is C++ code which will not block socket to read giving bytes

from socks5.

txthinking avatar txthinking commented on August 11, 2024

Please fork and https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request-from-a-fork

from socks5.

txthinking avatar txthinking commented on August 11, 2024

Does this useful?
https://stackoverflow.com/questions/12773509/read-is-not-blocking-in-socket-programming

from socks5.

txthinking avatar txthinking commented on August 11, 2024

I recommend use this go socks5 lib to build client.

Build and Use Go Packages as C Libraries

https://medium.com/swlh/build-and-use-go-packages-as-c-libraries-889eb0c19838

from socks5.

grayscott avatar grayscott commented on August 11, 2024

ok, 3Q

from socks5.

txthinking avatar txthinking commented on August 11, 2024

I made a c client to connect to the above server,

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
        int fd = 0;
        struct sockaddr_in a;
        if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        {
                return 1;
        }
        memset(&a, '0', sizeof(a));
        a.sin_family = AF_INET;
        a.sin_port = htons(9999);
        if(inet_pton(AF_INET, "127.0.0.1", &a.sin_addr)<=0){
                return 1;
        }
        if( connect(fd, (struct sockaddr *)&a, sizeof(a)) < 0){
                return 1;
        }

        int got = 0, n = 0;
        char b[2];
        memset(b, '0',sizeof(b));
        for(;n < 2;){
        n = read(fd, b+got, 2-got);
        if (n == 0){
            printf("closed\n");
            break;
        }
        if (n < -1){
            printf("error\n");
            break;
        }
        printf("read from server length %d\n", n);
        got += n;
        }
        return 0;
}

Run multi times:

// gcc a.c -o a
firsts-MacBook-Pro:Downloads tx$ ./a
read from server length 1
read from server length 1
closed
firsts-MacBook-Pro:Downloads tx$ ./a
read from server length 2
firsts-MacBook-Pro:Downloads tx$ ./a
read from server length 1
read from server length 1
closed
firsts-MacBook-Pro:Downloads tx$ ./a
read from server length 2
firsts-MacBook-Pro:Downloads tx$ ./a
read from server length 2
firsts-MacBook-Pro:Downloads tx$ ./a
read from server length 2
firsts-MacBook-Pro:Downloads tx$ ./a
read from server length 2
firsts-MacBook-Pro:Downloads tx$ ./a
read from server length 2
firsts-MacBook-Pro:Downloads tx$ ./a
read from server length 2
firsts-MacBook-Pro:Downloads tx$ ./a
read from server length 1
read from server length 1
closed

Every things is ok. so I closed this issue

from socks5.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.