Skip to content

Commit

Permalink
http3
Browse files Browse the repository at this point in the history
  • Loading branch information
txthinking committed Sep 26, 2022
1 parent 36213a9 commit 368cc8b
Show file tree
Hide file tree
Showing 8 changed files with 1,173 additions and 39 deletions.
758 changes: 758 additions & 0 deletions OPENSOURCELICENSES

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ go 1.16
require (
github.com/didip/tollbooth v4.0.2+incompatible
github.com/joho/godotenv v1.4.0
github.com/lucas-clemente/quic-go v0.29.1
github.com/txthinking/brook v0.0.0-20211202113055-0490a875bd14
github.com/txthinking/runnergroup v0.0.0-20210608031112-152c7c4432bf
github.com/urfave/negroni v1.0.0
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
Expand Down
278 changes: 276 additions & 2 deletions go.sum

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright (c) 2020-present Cloud <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package main

func init() {
Expand Down
26 changes: 22 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/joho/godotenv"
"github.com/txthinking/brook/limits"
"github.com/txthinking/runnergroup"
)

var maxbody int64 = 0
Expand Down Expand Up @@ -122,27 +123,44 @@ Env variables:
Also supoorts dotenv on /root/.nico.env
Verson:
v20220420
v20220926
Copyright:
https://github.com/txthinking/nico
`)
return
}

ss, err := Server(os.Args[1:])
h2, h3, err := Server(os.Args[1:])
if err != nil {
log.Println(err)
os.Exit(1)
return
}
g := runnergroup.New()
g.Add(&runnergroup.Runner{
Start: func() error {
return h2.ListenAndServeTLS("", "")
},
Stop: func() error {
return h2.Shutdown(context.Background())
},
})
g.Add(&runnergroup.Runner{
Start: func() error {
return h3.ListenAndServe()
},
Stop: func() error {
return h3.Close()
},
})
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
ss.Shutdown(context.Background())
g.Done()
}()
if err := ss.ListenAndServeTLS("", ""); err != nil {
if err := g.Wait(); err != nil {
log.Println(err)
os.Exit(1)
return
Expand Down
85 changes: 52 additions & 33 deletions server.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright (c) 2020-present Cloud <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package main

import (
Expand All @@ -15,30 +29,31 @@ import (

"github.com/didip/tollbooth"
"github.com/didip/tollbooth/limiter"
"github.com/lucas-clemente/quic-go/http3"
"github.com/urfave/negroni"
"golang.org/x/crypto/acme/autocert"
)

func Server(ll []string) (*http.Server, error) {
func Server(ll []string) (*http.Server, *http3.Server, error) {
nico := NewNico()
if strings.Contains(ll[0], " ") {
for _, v := range ll {
l := strings.Split(strings.TrimSpace(v), " ")
if len(l) != 2 {
return nil, errors.New("Invalid format: " + v)
return nil, nil, errors.New("Invalid format: " + v)
}
if err := nico.Add(l[0], l[1]); err != nil {
return nil, err
return nil, nil, err
}
}
}
if !strings.Contains(ll[0], " ") {
if len(ll)%2 != 0 {
return nil, errors.New("The number of parameters should be even")
return nil, nil, errors.New("The number of parameters should be even")
}
for i := 0; i < len(ll); i = i + 2 {
if err := nico.Add(ll[i], ll[i+1]); err != nil {
return nil, err
return nil, nil, err
}
}
}
Expand Down Expand Up @@ -75,16 +90,16 @@ func Server(ll []string) (*http.Server, error) {
for _, v := range nico.Domains() {
c, err := ioutil.ReadFile(filepath.Join(certpath, v+".cert.pem"))
if err != nil && !os.IsNotExist(err) {
return nil, err
return nil, nil, err
}
k, err := ioutil.ReadFile(filepath.Join(certpath, v+".key.pem"))
if err != nil && !os.IsNotExist(err) {
return nil, err
return nil, nil, err
}
if c != nil && k != nil {
ct, err := tls.X509KeyPair(c, k)
if err != nil {
return nil, err
return nil, nil, err
}
certs[v] = &ct
if net.ParseIP(v) != nil {
Expand All @@ -95,16 +110,16 @@ func Server(ll []string) (*http.Server, error) {
if strings.Index(v, ".") != -1 {
c, err := ioutil.ReadFile(filepath.Join(certpath, v[strings.Index(v, "."):]+".cert.pem"))
if err != nil && !os.IsNotExist(err) {
return nil, err
return nil, nil, err
}
k, err := ioutil.ReadFile(filepath.Join(certpath, v[strings.Index(v, "."):]+".key.pem"))
if err != nil && !os.IsNotExist(err) {
return nil, err
return nil, nil, err
}
if c != nil && k != nil {
ct, err := tls.X509KeyPair(c, k)
if err != nil {
return nil, err
return nil, nil, err
}
certs[v] = &ct
continue
Expand All @@ -123,27 +138,31 @@ func Server(ll []string) (*http.Server, error) {
}
go http.ListenAndServe(":80", m.HTTPHandler(nil))
}
return &http.Server{
Addr: ":" + strconv.FormatInt(port, 10),
ReadTimeout: time.Duration(timeout) * time.Second,
WriteTimeout: time.Duration(timeout) * time.Second,
IdleTimeout: time.Duration(timeout) * time.Second,
MaxHeaderBytes: 1 << 20,
Handler: n,
ErrorLog: log.New(&tlserr{}, "", log.LstdFlags),
TLSConfig: &tls.Config{
Certificates: l,
GetCertificate: func(c *tls.ClientHelloInfo) (*tls.Certificate, error) {
log.Printf("%#v\n", c)
v, ok := certs[c.ServerName]
if ok {
return v, nil
}
if len(auto) != 0 {
return m.GetCertificate(c)
}
return nil, errors.New("Not found " + c.ServerName)
},
tc := &tls.Config{
Certificates: l,
GetCertificate: func(c *tls.ClientHelloInfo) (*tls.Certificate, error) {
v, ok := certs[c.ServerName]
if ok {
return v, nil
}
if len(auto) != 0 {
return m.GetCertificate(c)
}
return nil, errors.New("Not found " + c.ServerName)
},
}, nil
}
return &http.Server{
Addr: ":" + strconv.FormatInt(port, 10),
ReadTimeout: time.Duration(timeout) * time.Second,
WriteTimeout: time.Duration(timeout) * time.Second,
IdleTimeout: time.Duration(timeout) * time.Second,
MaxHeaderBytes: 1 << 20,
Handler: n,
ErrorLog: log.New(&tlserr{}, "", log.LstdFlags),
TLSConfig: tc,
}, &http3.Server{
Addr: ":" + strconv.FormatInt(port, 10),
TLSConfig: tc,
Handler: n,
}, nil
}
35 changes: 35 additions & 0 deletions test_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2016-present Cloud <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package main

import (
"log"
"net/http"
"testing"

"github.com/lucas-clemente/quic-go/http3"
)

func TestTest(t *testing.T) {
hc := http.Client{
Transport: &http3.RoundTripper{},
}
res, err := hc.Get("https://test.txthinking.com")
if err != nil {
log.Println(err)
return
}
log.Println(res.StatusCode, res.Proto)
}
14 changes: 14 additions & 0 deletions tlserr.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright (c) 2020-present Cloud <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package main

import (
Expand Down

0 comments on commit 368cc8b

Please sign in to comment.