Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set nhp-token cookie #1217

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type HttpConfig struct {
HttpListenIp string
TLSCertFile string
TLSKeyFile string
AccessControlAllowOrigin string
}

type Peers struct {
Expand Down
8 changes: 5 additions & 3 deletions server/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type HttpServer struct {
ginEngine *gin.Engine
listenAddr *net.TCPAddr

accessControlAllowOrigin string

wg sync.WaitGroup
running atomic.Bool

Expand Down Expand Up @@ -67,7 +69,7 @@ func (hs *HttpServer) Start(us *UdpServer, hc *HttpConfig) error {

gin.SetMode(gin.ReleaseMode)
hs.ginEngine = gin.New()
hs.ginEngine.Use(corsMiddleware())
hs.ginEngine.Use(corsMiddleware(hc.AccessControlAllowOrigin))
hs.ginEngine.Use(gin.LoggerWithWriter(us.log.Writer()))
hs.ginEngine.Use(gin.Recovery())

Expand Down Expand Up @@ -282,10 +284,10 @@ func (hs *HttpServer) initRouter() {
// corsMiddleware is a middleware function that adds CORS headers to the HTTP response.
// It allows cross-origin resource sharing, specifies allowed methods, exposes headers, and sets maximum age.
// If the request method is OPTIONS, PUT, or DELETE, it aborts the request with a 204 status code.
func corsMiddleware() gin.HandlerFunc {
func corsMiddleware(originResource string) gin.HandlerFunc {
return func(c *gin.Context) {
// HTTP headers for CORS
c.Writer.Header().Set("Access-Control-Allow-Origin", "*") // allow cross-origin resource sharing
c.Writer.Header().Set("Access-Control-Allow-Origin", originResource) // allow cross-origin resource sharing
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS, POST") // methods
c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Type, Content-Length, Set-Cookie")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Authorization, X-NHP-Ver, Cookie")
Expand Down
2 changes: 2 additions & 0 deletions server/main/etc/http.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
# EnableTLS: whether to use TLS certificates for hosting https server.
# TLSCertFile: certificate file path.
# TLSKeyFile: key file path.
# AccessControlAllowOrigin: the response header indicates whether the response can be shared with requesting code from the given origin.
# to update http changes, you need to restart the http server by changing "EnableHttp" to "false" and then switch it back to "true".
EnableHttp = true
EnableTLS = true
HttpListenIp = "0.0.0.0" # empty for ipv4 + ipv6, "0.0.0.0" for ipv4 only, "127.0.0.1" for local ipv4 access only
TLSCertFile = "cert/cert.pem"
TLSKeyFile = "cert/cert.key"
AccessControlAllowOrigin = "https://demologin.opennhp.cn"
4 changes: 3 additions & 1 deletion server/plugins/example/templates/example_login.html
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ <h2 id="authSuccessMessage"></h2>
"&password=" + encodeURIComponent(password);
console.log(nhpValidUrl);

fetch(nhpValidUrl)
fetch(nhpValidUrl,{
credentials: "include"
})
.then(response => response.json())
.then(result => {
console.log(result);
Expand Down
Loading