Skip to content

Commit

Permalink
use quic protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
xinran0722l committed Aug 2, 2022
1 parent 6babf8e commit fe2b82b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 33 deletions.
Binary file added demo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions ethrs.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"Time":"2022-07-18T16:52:23Z","Title":"","Type":"INFO","Message":"Accepting IP version: ipv4, ipv6"}
{"Time":"2022-07-18T16:52:23Z","Title":"","Type":"INFO","Message":"Listening on port 8888 for TCP \u0026 UDP"}
46 changes: 34 additions & 12 deletions garnish/garnish.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"net/http/httputil"
"net/url"
"time"
"crypto/tls"
"io/ioutil"

)

const Xcache = "X-Cache"
Expand Down Expand Up @@ -72,36 +75,55 @@ func (g *garnish) ServeHTTP(rw http.ResponseWriter, r *http.Request, serverAddre
return
}
// garnish connect to the server
conn, err := pan.DialUDP(context.Background(), netaddr.IPPort{}, addr, nil, nil)
//conn, err := pan.DialUDP(context.Background(), netaddr.IPPort{}, addr, nil, nil)
tlsCfg := &tls.Config{
InsecureSkipVerify: true,
NextProtos: []string{"hello-quic"},
}
// Set Pinging Selector with active probing on two paths
selector := &pan.PingingSelector{
Interval: 2 * time.Second,
Timeout: time.Second,
}
selector.SetActive(2)
session, err := pan.DialQUIC(context.Background(), netaddr.IPPort{}, addr, nil, selector, "", tlsCfg, nil)
if err != nil {
fmt.Println("connect to server error")
return
}
defer conn.Close()
//defer conn.Close()

nBytes, err := conn.Write([]byte(fmt.Sprintf("garnish message")))
//nBytes, err := conn.Write([]byte(fmt.Sprintf("garnish message")))
stream, err := session.OpenStream()
if err != nil {
fmt.Print(nBytes)
fmt.Println(err)
}

buffer := make([]byte, 16*1024)
if err = conn.SetReadDeadline(time.Now().Add(1 * time.Second)); err != nil {
fmt.Println("SetReadDeadline error")
return
_, err = stream.Write([]byte(fmt.Sprintf("Welcome to Scion")))

if err != nil {
fmt.Println(err)
}
n, err := conn.Read(buffer) //read to this buffer

//
stream.Close()
//buffer := make([]byte, 16*1024)
// if err = conn.SetReadDeadline(time.Now().Add(1 * time.Second)); err != nil {
// fmt.Println("SetReadDeadline error")
// return
// }
//n, err := conn.Read(buffer) //read to this buffer
data, err := ioutil.ReadAll(stream)
if err != nil {
fmt.Println("here")
fmt.Println(err)
//return
}
data := buffer[:n]
//data := buffer[:n]
duration := time.Duration(123) * time.Second
g.c.store(u, data, duration)
_, _ = rw.Write(data)
fmt.Println("Store data ")
return
// fmt.Println(data)

}
74 changes: 53 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package main

import (
"context"
"crypto/tls"
"flag"
"fmt"
"log"
"errors"
"net/http"
"net/url"
//"time"

"github.com/lucas-clemente/quic-go"
"io/ioutil"
"inet.af/netaddr"

"github.com/bkielbasa/garnish/garnish"
"github.com/netsec-ethz/scion-apps/pkg/pan"
"github.com/netsec-ethz/scion-apps/pkg/quicutil"
)

func main() {
Expand Down Expand Up @@ -66,21 +68,19 @@ func runGarnish(address string) {
// }
// }

//change into scion
func runServer(listen netaddr.IPPort) {
conn, err := pan.ListenUDP(context.Background(), listen, nil)
if err != nil {
fmt.Printf("listen error")
}
//defer conn.Close()
fmt.Print("Hello! ")
fmt.Println(conn.LocalAddr())
for true {
buffer := make([]byte, 1024*16*1024)
n, from, err := conn.ReadFrom(buffer)
// create work session
func workSession(session quic.Session) error {
for {
stream, err := session.AcceptStream(context.Background())
if err != nil {
fmt.Printf("read error")
return err
}
defer stream.Close()
data, err := ioutil.ReadAll(stream)
if err != nil {
return err
}
fmt.Printf("%s\n", data)
const msg = `<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -93,14 +93,46 @@ func runServer(listen netaddr.IPPort) {
<img src="https://www.cylab.cmu.edu/_files/images/research/scion/scion-banner.png" alt="SCION banner">
</body>
</html>`
//msg := fmt.Sprintf("Aaaaa, i love it")
n, err = conn.WriteTo([]byte(msg), from)
_, err = stream.Write([]byte(msg))
if err != nil {
return err
}
_, err = stream.Write(data)
if err != nil {
fmt.Printf("write error")
return err
}
fmt.Printf("Wrote %d bytes.\n", n)
//time.Sleep(time.Millisecond * 30)
stream.Close()
}
}
//change into scion
func runServer(listen netaddr.IPPort) {
//conn, err := pan.ListenUDP(context.Background(), listen, nil)
tlsCfg := &tls.Config{
Certificates: quicutil.MustGenerateSelfSignedCert(),
NextProtos: []string{"hello-quic"},
}

listener, err := pan.ListenQUIC(context.Background(), listen, nil, tlsCfg, nil)
if err != nil {
fmt.Printf("listen error")
}
defer listener.Close()
//18-ffaa:1:f53,127.0.0.1:1234
fmt.Println(listener.Addr())
for {
session, err := listener.Accept(context.Background())
if err != nil {
fmt.Printf("listener accept error")
}
fmt.Println("New session", session.RemoteAddr())
go func() {
err := workSession(session)
var errApplication *quic.ApplicationError
if err != nil && !(errors.As(err, &errApplication) && errApplication.ErrorCode == 0) {
fmt.Println(session.RemoteAddr())
}
}()
}
}

func panicOnErr(err error) {
Expand Down
Empty file added web.html
Empty file.

0 comments on commit fe2b82b

Please sign in to comment.