()
| 42 | } |
| 43 | |
| 44 | func Main() error { |
| 45 | var args struct { |
| 46 | Port string `default:":19870"` |
| 47 | } |
| 48 | arg.MustParse(&args) |
| 49 | |
| 50 | root, err := certin.NewCert(nil, certin.Request{CN: "root CA", IsCA: true}) |
| 51 | if err != nil { |
| 52 | return fmt.Errorf("error creating root CA: %w", err) |
| 53 | } |
| 54 | |
| 55 | // write the certificate authority to a temporary file |
| 56 | err = writeCertFile(root.Certificate.Raw, "ca.crt") |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | // leaf, err := certin.NewCert(root, certin.Request{ |
| 62 | // CN: "example.com", |
| 63 | // SANs: []string{"example.com", "www.example.com", "127.0.0.1"}, |
| 64 | // }) |
| 65 | // if err != nil { |
| 66 | // return fmt.Errorf("error creating leaf certificate: %w", err) |
| 67 | // } |
| 68 | |
| 69 | // write the server certificate to a temporary file |
| 70 | // err = writeCertFile(leaf.Certificate.Raw, "certificate.crt") |
| 71 | // if err != nil { |
| 72 | // return err |
| 73 | // } |
| 74 | |
| 75 | // start an HTTP server |
| 76 | const plaintext = "hello httptap world" |
| 77 | server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 78 | fmt.Fprintln(w, plaintext) |
| 79 | })) |
| 80 | server.TLS = &tls.Config{ |
| 81 | GetCertificate: func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) { |
| 82 | log.Printf("got challenge for %q", hello.ServerName) |
| 83 | onthefly, err := certin.NewCert(root, certin.Request{CN: hello.ServerName}) |
| 84 | if err != nil { |
| 85 | log.Println("error creating cert: %w", err) |
| 86 | return nil, fmt.Errorf("error creating on-the-fly certificate for %q: %w", hello.ServerName, err) |
| 87 | } |
| 88 | |
| 89 | err = writeCertFile(onthefly.Certificate.Raw, "certificate.crt") |
| 90 | if err != nil { |
| 91 | log.Printf("error writing on-the-fly certificate to file: %v, ignoring", err) |
| 92 | } |
| 93 | |
| 94 | tlscert := onthefly.TLSCertificate() |
| 95 | return &tlscert, nil |
| 96 | }, |
| 97 | } |
| 98 | server.Listener, err = net.Listen("tcp", args.Port) |
| 99 | if err != nil { |
| 100 | return fmt.Errorf("unable to listen on %v: %w", args.Port, err) |
| 101 | } |
no test coverage detected