package network

import (
	"crypto/tls"
	"net"
)

const protocol = "tcp"

func CreateTLSListener(addrinfo string, x509cert, x509key []byte) (net.Listener, error) {
	var listen net.Listener
	var err error
	tlsCert, err := tls.X509KeyPair(x509cert, x509key)
	if err != nil {
		return listen, err
	}
	tlsConfig := tls.Config{
		Certificates:       []tls.Certificate{tlsCert},
		ClientAuth:         tls.NoClientCert,
		InsecureSkipVerify: true,
	}
	listen, err = tls.Listen(protocol, addrinfo, &tlsConfig)
	if err != nil {
		return listen, err
	}
	return listen, err
}

func CreateListener(addrinfo string) (net.Listener, error) {
	var listen net.Listener
	var err error
	listen, err = net.Listen(protocol, addrinfo)
	if err != nil {
		return listen, err
	}
	return listen, err
}
