GithubHelp home page GithubHelp logo

Comments (1)

bo-er avatar bo-er commented on July 16, 2024

The reason why I wasn't observing any issue is that I was starting mqtt server as what is shown in the example.
And to run a tls supported gmqtt server you should write your code like this:
Feel free to ask me for help!

	tlsCfg, err := buildTLSConfig(c.TLSOptions)
			if err != nil {
				logger.Fatalf("failed to build tls config:%v", err)
			}
			listener, err = tls.Listen("tcp", c.Address, tlsCfg)
			logger.Infof("mqtt will listen on port :%v in tls mode", c.Address)
			if err != nil {
				logger.Fatalf("failed to creates a TLS listener:%v", err)
			}

		} else {
			var err error
			listener, err = net.Listen("tcp", c.Address)
			if err != nil {
				panic(err)
			}
		}
func buildTLSConfig(cfg *config.TLSOptions) (*tls.Config, error) {
	c, err := tls.LoadX509KeyPair(cfg.Cert, cfg.Key)
	if err != nil {
		return nil, err
	}
	certPool := x509.NewCertPool()
	if cfg.CACert != "" {
		b, err := ioutil.ReadFile(cfg.CACert)
		if err != nil {
			return nil, err
		}
		certPool.AppendCertsFromPEM(b)
	}
	var cliAuthType tls.ClientAuthType
	if cfg.Verify {
		cliAuthType = tls.RequireAndVerifyClientCert
	}
	tlsCfg := &tls.Config{
		Certificates: []tls.Certificate{c},

		//servers clientCA to verify a client certificate
		ClientCAs: certPool,
		// ClientAuth determines the server's policy for
		// TLS Client Authentication.
		ClientAuth: cliAuthType,
	}
	return tlsCfg, nil
}

from gmqtt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.