Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: brewlin/net-protocol
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.1.0
Choose a base ref
...
head repository: brewlin/net-protocol
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref

Commits on Dec 19, 2019

  1. [udp-client] #add udp-client

    brewlin committed Dec 19, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    d9d6f58 View commit details

Commits on Dec 25, 2019

  1. Copy the full SHA
    baebd7f View commit details
  2. [udp client] #update

    brewlin committed Dec 25, 2019
    Copy the full SHA
    223b6eb View commit details
  3. update readme

    brewlin committed Dec 25, 2019
    Copy the full SHA
    f9771aa View commit details

Commits on Mar 4, 2020

  1. #add dns protocol first

    brewlin committed Mar 4, 2020
    Copy the full SHA
    703cee2 View commit details
  2. Copy the full SHA
    ee7afb7 View commit details

Commits on Mar 5, 2020

  1. Copy the full SHA
    2bb38cb View commit details
  2. Fix #2

    brewlin committed Mar 5, 2020
    Copy the full SHA
    ed27346 View commit details
  3. Fix #1

    brewlin committed Mar 5, 2020
    Copy the full SHA
    3605900 View commit details
  4. [upd-client] #update comment

    brewlin committed Mar 5, 2020
    Copy the full SHA
    370a9e8 View commit details
  5. Copy the full SHA
    99e8ec3 View commit details

Commits on Mar 6, 2020

  1. [dns] #support dns lookup

    brewlin committed Mar 6, 2020
    Copy the full SHA
    0c817c4 View commit details

Commits on Mar 7, 2020

  1. Copy the full SHA
    a9a7fce View commit details
  2. [dns] #done dns server demo

    brewlin committed Mar 7, 2020
    Copy the full SHA
    0f992a4 View commit details
  3. update readme

    brewlin committed Mar 7, 2020
    Copy the full SHA
    47dc180 View commit details

Commits on Mar 9, 2020

  1. update readme

    brewlin committed Mar 9, 2020
    Copy the full SHA
    cc4d277 View commit details

Commits on Mar 25, 2020

  1. [syscall] #add lxc net

    brewlin committed Mar 25, 2020
    Copy the full SHA
    a57f037 View commit details

Commits on Mar 26, 2020

  1. Copy the full SHA
    01964d0 View commit details

Commits on Aug 10, 2020

  1. update readme

    brewlin committed Aug 10, 2020
    Copy the full SHA
    86e4fcc View commit details

Commits on Feb 26, 2021

  1. remove irrelevant files

    brewlin committed Feb 26, 2021
    Copy the full SHA
    cf36fae View commit details

Commits on Dec 11, 2021

  1. [test] fix require libs

    brewlin authored and poulsenliu committed Dec 11, 2021
    Copy the full SHA
    3f5c629 View commit details
  2. [test] udp client by golang/net

    brewlin authored and poulsenliu committed Dec 11, 2021
    Copy the full SHA
    cacae12 View commit details

Commits on Dec 13, 2021

  1. Copy the full SHA
    ec64e5f View commit details

Commits on Oct 13, 2022

  1. Update README.md

    brewlin authored Oct 13, 2022
    Copy the full SHA
    cc1b50d View commit details
Showing with 3,836 additions and 400 deletions.
  1. +34 −0 Dockerfile
  2. +67 −56 README.md
  3. +26 −0 cmd/application/dns/README.md
  4. +29 −0 cmd/application/dns/dns_client.go
  5. +116 −0 cmd/application/dns/dns_server.go
  6. +4 −1 cmd/transport/tcp/client.go
  7. +7 −105 cmd/transport/tcp/server/main.go
  8. +16 −26 cmd/transport/udp/client/main.go
  9. +39 −0 cmd/transport/udp/net_client.go
  10. +17 −145 cmd/transport/udp/server/main.go
  11. +12 −5 config/net.go
  12. +78 −0 internal/endpoint/endpoint.go
  13. +7 −0 pkg/buffer/view.go
  14. +588 −0 pkg/checker/checker.go
  15. +3 −0 pkg/logging/log.go
  16. +5 −0 pkg/reexec/README.md
  17. +28 −0 pkg/reexec/command_linux.go
  18. +23 −0 pkg/reexec/command_unix.go
  19. +12 −0 pkg/reexec/command_unsupported.go
  20. +23 −0 pkg/reexec/command_windows.go
  21. +47 −0 pkg/reexec/reexec.go
  22. +42 −0 pkg/syscall/c_net/net.go
  23. +23 −0 pkg/syscall/c_net/net_test.go
  24. +712 −0 pkg/syscall/c_net/network.c
  25. +34 −0 pkg/syscall/c_net/network.h
  26. +282 −0 pkg/syscall/c_net/nl.c
  27. +239 −0 pkg/syscall/c_net/nl.h
  28. +101 −0 pkg/syscall/pivot_root.go
  29. +53 −0 protocol/application/dns/endopoint.go
  30. +15 −0 protocol/application/dns/query.go
  31. +57 −0 protocol/application/dns/rsp.go
  32. +204 −0 protocol/header/dns.go
  33. +125 −0 protocol/link/channel/channel.go
  34. +66 −0 protocol/link/sniffer/pcap.go
  35. +390 −0 protocol/link/sniffer/sniffer.go
  36. +4 −2 protocol/network/arp/arp.go
  37. +4 −4 protocol/network/arp/arp_test.go
  38. +3 −3 protocol/network/ip_test.go
  39. +5 −5 protocol/network/ipv4/ip.go
  40. +4 −4 protocol/network/ipv4/ipv4_test.go
  41. +5 −5 protocol/network/ipv6/icmp_test.go
  42. +3 −0 protocol/tcpip.go
  43. +3 −3 protocol/transport/tcp/dual_stack_test.go
  44. +7 −7 protocol/transport/tcp/tcp_test.go
  45. +3 −3 protocol/transport/tcp/tcp_timestamp_test.go
  46. +6 −6 protocol/transport/tcp/testing/context/context.go
  47. +100 −0 protocol/transport/udp/client/client.go
  48. +23 −0 protocol/transport/udp/client/get.go
  49. +59 −0 protocol/transport/udp/client/read.go
  50. +19 −0 protocol/transport/udp/client/write.go
  51. +29 −1 protocol/transport/udp/endpoint.go
  52. +6 −6 protocol/transport/udp/udp_test.go
  53. BIN resource/dns_client.png
  54. BIN resource/dns_server.png
  55. +2 −2 stack/stack_test.go
  56. +7 −2 stack/stackinit/init.go
  57. +3 −3 stack/transport_test.go
  58. +17 −6 tool/up.go
34 changes: 34 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM centos:7

LABEL maintainer="brewlin" version="1.0" license="MIT"

RUN yum install -y gcc-c++

ENV PATH /usr/local/go/bin:$PATH
ENV GOROOT /usr/local/go
ENV GOPATH /home/go

RUN yum -y install wget \
&& mkdir /home/go \
&& wget https://studygolang.com/dl/golang/go1.13.10.linux-amd64.tar.gz \
&& tar -C /usr/local -zxf go1.13.10.linux-amd64.tar.gz \
&& yum -y install iproute net-tools

RUN echo export GOROOT=/usr/local/go >> /etc/profile
RUN echo export GOPATH=/home/go >> /etc/profile
RUN echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile
RUN rm -f go1.13.10.linux-amd64.tar.gz
RUN source /etc/profile && go version
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo 'Asia/Shanghai' >/etc/timezone

ADD . /test/net-protocol

RUN cd /test/net-protocol \
&& cd tool \
&& go build -x up.go \
&& ./up

WORKDIR /test/net-protocol

CMD [ "/test/net-protocol/tool/up"]
123 changes: 67 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
@@ -7,82 +7,93 @@
</p>


基于go 实现链路层、网络层、传输层、应用层 网络协议栈 ,使用虚拟网卡实现
## @docs
基于go 实现链路层、网络层、传输层、应用层 网络协议栈 ,使用虚拟网卡实现 docs: https://wiki.brewlin.com/wiki/github/net-protocol/index/
## @demo && test
相关demo以及协议测试在cmd目录下,`cd ./cmd*`

docker
```
相关md文档在cmd目录下,以及相关协议的demo测试
> docker run -d --device /dev/net/tun:/dev/net/tun --privileged brewlin/net-protocol
> docker exec -it containerid /bin/bash
> go run cmd/transport/udp/server/main.go
> go run cmd/transport/udp/net_client.go
```
`./cmd/*.md`
## @application 应用层
- [x] http [docs](./cmd/http.md)
- [x] websocket [docs](./cmd/websocket.md)
- [x] [http](https://wiki.brewlin.com/wiki/github/net-protocol/index/)
- [x] [websocket](http://wiki.brewlin.com/wiki/github/net-protocol/index/)
- [x] [dns](http://wiki.brewlin.com/wiki/github/net-protocol/index/)


## @transport 传输层
- [x] tcp [docs](./cmd/tcp.md)
- [x] udp [docs](./cmd/udp.md)
- [x] port 端口机制
- [x] [tcp](http://wiki.brewlin.com/wiki/github/net-protocol/index/)
- [x] [udp](http://wiki.brewlin.com/wiki/github/net-protocol/index/)
- [x] [port](http://wiki.brewlin.com/wiki/github/net-protocol/index/) 端口机制

## @network 网络层
- [x] icmp
- [x] ipv4
- [x] ipv6
- [x] [icmp](http://wiki.brewlin.com/wiki/github/net-protocol/index/)
- [x] [ipv4](http://wiki.brewlin.com/wiki/github/net-protocol/index/)
- [x] [ipv6](http://wiki.brewlin.com/wiki/github/net-protocol/index/)

## @link 链路层
- [x] arp [docs](./cmd/arp.md)
- [x] ethernet
- [x] [arp](http://wiki.brewlin.com/wiki/github/net-protocol/index/)
- [x] [ethernet](http://wiki.brewlin.com/wiki/github/net-protocol/index/)

## @物理层
- [x] tun tap 虚拟网卡的实现
- [x] tun [tap](http://wiki.brewlin.com/wiki/github/net-protocol/index/) 虚拟网卡的实现

## @客户端
发起客户端请求
- [x] http client
- [x] websocket client
- [x] tcp client
## 协议相关api
- [x] [http client](http://wiki.brewlin.com/wiki/github/net-protocol/index/)
- [x] [websocket client](http://wiki.brewlin.com/github/wiki/net-protocol/index/)
- [x] [tcp client](http://wiki.brewlin.com/wiki/github/net-protocol/index/)
- [x] [udp client](http://wiki.brewlin.com/wiki/github/net-protocol/index/)
- [x] [dns client](http://wiki.brewlin.com/wiki/github/net-protocol/index/)
## 协议相关构体
具体细节文档:http://wiki.brewlin.com/wiki/github/net-protocol/index/
### 1.应用层相关协议
应用层暂时只实现了`http``websocket`等文本协议。都基于tcp、对tcp等进行二次封装
应用层暂时只实现了`http``websocket``dns`协议。都基于tcp、udp等进行二次封装

[http api](./http-api.md) :`http-api.md`
http protocol:
```
http 协议报文
GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin: http://example.com
Sec-WebSocket-Protcol: chat, superchat
Sec-WebSocket-Version: 13
http 协议报文
GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Origin: http://example.com
Sec-WebSocket-Protcol: chat, superchat
Sec-WebSocket-Version: 13
```
[websocket api](./websocket-api.md) : `websocket-api.md`
websocket protocol:
```
websocket 数据帧报文
0 1 2 3 4
0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
| Extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - - +-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------- - - - - - - - - - - - - - - - +
: Payload Data continued ... :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Payload Data continued ... |
+---------------------------------------------------------------+
websocket 数据帧报文
0 1 2 3 4
0 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
| Extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - - +-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------- - - - - - - - - - - - - - - - +
: Payload Data continued ... :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Payload Data continued ... |
+---------------------------------------------------------------+
```
### 2.传输层相关协议
传输层实现了`upd``tcp`灯协议,并实现了主要接口
传输层实现了`upd``tcp`等协议,实现了主要接口

[tcp api](./tcp-api.md):`tcp-api.md`
tcp protocol:

```
tcp 首部协议报文
@@ -107,7 +118,7 @@
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
```

[udp-api](./udp-api.md):`./udp-api.md`
udp-protocol:
```
udp 协议报文
```
@@ -117,7 +128,7 @@ udp 协议报文

### 3.网络层相关协议

[ip](./ipv-api.md):`ipv4-api.md`
ip protocol:
```
ip头部协议报文
0 1 2 3 4
@@ -137,4 +148,4 @@ udp 协议报文
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options (*** bits) | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
```
```
26 changes: 26 additions & 0 deletions cmd/application/dns/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[toc]
# DNS ClIENT
```
> cd net-protocol/tool;
> go build up.go
> sudo ./up
> cd net-protocol/cmd/application/dns
> sudo go run dns_client.go
```
![](../../../resource/dns_client.png)


# DNS SERVER
启动 udp server,另起窗口发送dns查询并指定dnsserver
```
> cd net-protocol/tool;
> go build up.go
> sudo ./up
> cd net-protocol/cmd/application/dns
> sudo go run dns_server.go
//另起ssh窗口 发送dns查询并指定自定义的dns server 192.168.1.1:53
> nslookup www.baidu.com 192.168.1.1
```
![](../../../resource/dns_server.png)
29 changes: 29 additions & 0 deletions cmd/application/dns/dns_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"github.com/brewlin/net-protocol/protocol/application/dns"
"github.com/brewlin/net-protocol/protocol/header"
)

func main() {
d := dns.NewEndpoint("www.baidu.com")
fmt.Println("DNS lookuphost : www.baidu.com")
defer d.Close()

ir,err := d.Resolve();
if err != nil {
fmt.Println(err)
return
}
for _,v := range *ir {
switch v.Type {
case header.A:
fmt.Println("A(host name) :",v.Address)
case header.CNAME:
fmt.Println("CNAME (alias name):",v.Address)
}
}


}
116 changes: 116 additions & 0 deletions cmd/application/dns/dns_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

import (
"fmt"
"github.com/brewlin/net-protocol/config"
"github.com/brewlin/net-protocol/internal/endpoint"
"github.com/brewlin/net-protocol/pkg/buffer"
_ "github.com/brewlin/net-protocol/pkg/logging"
_ "github.com/brewlin/net-protocol/pkg/logging"
"github.com/brewlin/net-protocol/pkg/waiter"
"github.com/brewlin/net-protocol/protocol/header"
"github.com/brewlin/net-protocol/protocol/network/ipv4"
"github.com/brewlin/net-protocol/protocol/transport/udp"
"github.com/brewlin/net-protocol/protocol/transport/udp/client"
"github.com/brewlin/net-protocol/stack"
"log"
"strconv"
"strings"

tcpip "github.com/brewlin/net-protocol/protocol"
)

//当前demo作为一个dns代理,接受dns请求并转发后,解析响应做一些操作
func main() {
s := endpoint.NewEndpoint()

udploop(s)

}
func udploop(s *stack.Stack) {
var wq waiter.Queue
//新建一个UDP端
ep, err := s.NewEndpoint(udp.ProtocolNumber, ipv4.ProtocolNumber, &wq)
if err != nil {
log.Fatal(err)
}
//绑定本地端口 53是dns默认端口
if err := ep.Bind(tcpip.FullAddress{1, config.LocalAddres, 53}, nil); err != nil {
log.Fatal("@main : bind failed :", err)
}
defer ep.Close()
//创建队列 通知 channel
waitEntry, notifych := waiter.NewChannelEntry(nil)
wq.EventRegister(&waitEntry, waiter.EventIn)
defer wq.EventUnregister(&waitEntry)

var saddr tcpip.FullAddress

for {
v, _, err := ep.Read(&saddr)
if err != nil {
if err == tcpip.ErrWouldBlock {
<-notifych
continue
}
fmt.Println(err)
return
}
//接收到代理请求
h := header.DNS(v)
fmt.Println("@main :接收到代理域名:", string(h[header.DOMAIN:header.DOMAIN+h.GetDomainLen()-1]))
go handle_proxy(v,ep,saddr)
}
}
//转发代理请求,并解析响应数据
func handle_proxy(v buffer.View,ep tcpip.Endpoint,saddr tcpip.FullAddress){
cli := client.NewClient("8.8.8.8",53)
cli.Connect()
cli.Write(v)
defer cli.Close()

rsp,err := cli.Read()
if err != nil {
fmt.Println(err)
return
}
//返回给客户端
_, _, err = ep.Write(tcpip.SlicePayload(rsp), tcpip.WriteOptions{To: &saddr})
if err != nil {
fmt.Println(err)
}
p := header.DNS(rsp)
answer := p.GetAnswer()

for i := 0; i < len(*answer) ; i++ {
switch (*answer)[i].Type {
case header.A:
fmt.Println("dns 目标IP(A):",parseAName((*answer)[i].RData))
case header.CNAME:
fmt.Println("dns 目标IP(alias):",parseCName((*answer)[i].RData))
}
}
}
func parseAName(rd []byte) string {
res := []string{}
for _,v := range rd {
res = append(res,strconv.Itoa(int(v)))
}
return strings.Join(res,".")
}

func parseCName(rd []byte) (res string) {
for{
l := int(rd[0])
if l >= len(rd){
res += ".com"
return
}
rd = rd[1:]
res += string(rd[0:l])
rd = rd[l:]
if len(rd) == 0 {
return
}
}
}
5 changes: 4 additions & 1 deletion cmd/transport/tcp/client.go
Original file line number Diff line number Diff line change
@@ -4,10 +4,13 @@ import (
"io/ioutil"
"log"
"net"
"strconv"

"github.com/brewlin/net-protocol/config"
)

func main() {
addr := "192.168.1.1:9111"
addr := config.LocalAddres.To4().String() + ":" + strconv.Itoa(int(config.LocalPort))
tcpaddr, err := net.ResolveTCPAddr("", addr)
if err != nil {
log.Fatal("net Resolvetcp addr error!", err.Error())
Loading