server.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package handler
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "io"
  6. "log"
  7. "net"
  8. "net/http"
  9. "os"
  10. "time"
  11. "github.com/bjdgyc/anylink/base"
  12. "github.com/bjdgyc/anylink/dbdata"
  13. "github.com/gorilla/mux"
  14. "github.com/pires/go-proxyproto"
  15. )
  16. func startTls() {
  17. var (
  18. err error
  19. addr = base.Cfg.ServerAddr
  20. ln net.Listener
  21. )
  22. // 判断证书文件
  23. // _, err = os.Stat(certFile)
  24. // if errors.Is(err, os.ErrNotExist) {
  25. // // 自动生成证书
  26. // certs[0], err = selfsign.GenerateSelfSignedWithDNS("vpn.anylink")
  27. // } else {
  28. // // 使用自定义证书
  29. // certs[0], err = tls.LoadX509KeyPair(certFile, keyFile)
  30. // }
  31. // 修复 CVE-2016-2183
  32. // https://segmentfault.com/a/1190000038486901
  33. // nmap -sV --script ssl-enum-ciphers -p 443 www.example.com
  34. cipherSuites := tls.CipherSuites()
  35. selectedCipherSuites := make([]uint16, 0, len(cipherSuites))
  36. for _, s := range cipherSuites {
  37. selectedCipherSuites = append(selectedCipherSuites, s.ID)
  38. }
  39. // 设置tls信息
  40. tlsConfig := &tls.Config{
  41. NextProtos: []string{"http/1.1"},
  42. MinVersion: tls.VersionTLS12,
  43. CipherSuites: selectedCipherSuites,
  44. GetCertificate: func(chi *tls.ClientHelloInfo) (*tls.Certificate, error) {
  45. base.Trace("GetCertificate", chi.ServerName)
  46. return dbdata.GetCertificateBySNI(chi.ServerName)
  47. },
  48. // InsecureSkipVerify: true,
  49. }
  50. srv := &http.Server{
  51. Addr: addr,
  52. Handler: initRoute(),
  53. TLSConfig: tlsConfig,
  54. ErrorLog: base.GetBaseLog(),
  55. ReadTimeout: 60 * time.Second,
  56. WriteTimeout: 60 * time.Second,
  57. }
  58. ln, err = net.Listen("tcp", addr)
  59. if err != nil {
  60. log.Fatal(err)
  61. }
  62. defer ln.Close()
  63. if base.Cfg.ProxyProtocol {
  64. ln = &proxyproto.Listener{
  65. Listener: ln,
  66. ReadHeaderTimeout: 30 * time.Second,
  67. }
  68. }
  69. base.Info("listen server", addr)
  70. err = srv.ServeTLS(ln, "", "")
  71. if err != nil {
  72. base.Fatal(err)
  73. }
  74. }
  75. func initRoute() http.Handler {
  76. r := mux.NewRouter()
  77. r.HandleFunc("/", LinkHome).Methods(http.MethodGet)
  78. r.HandleFunc("/", LinkAuth).Methods(http.MethodPost)
  79. r.HandleFunc("/CSCOSSLC/tunnel", LinkTunnel).Methods(http.MethodConnect)
  80. r.HandleFunc("/otp_qr", LinkOtpQr).Methods(http.MethodGet)
  81. r.HandleFunc("/profile.xml", func(w http.ResponseWriter, r *http.Request) {
  82. b, _ := os.ReadFile(base.Cfg.Profile)
  83. w.Write(b)
  84. }).Methods(http.MethodGet)
  85. r.PathPrefix("/files/").Handler(
  86. http.StripPrefix("/files/",
  87. http.FileServer(http.Dir(base.Cfg.FilesPath)),
  88. ),
  89. )
  90. // 健康检测
  91. r.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
  92. io.WriteString(w, "ok")
  93. }).Methods(http.MethodGet)
  94. r.NotFoundHandler = http.HandlerFunc(notFound)
  95. return r
  96. }
  97. func notFound(w http.ResponseWriter, r *http.Request) {
  98. // fmt.Println(r.RemoteAddr)
  99. // hu, _ := httputil.DumpRequest(r, true)
  100. // fmt.Println("NotFound: ", string(hu))
  101. w.WriteHeader(http.StatusNotFound)
  102. fmt.Fprintln(w, "404 page not found")
  103. }