Skip to content

Commit

Permalink
Add NeedRedirect to site
Browse files Browse the repository at this point in the history
  • Loading branch information
nomeguy committed Oct 6, 2023
1 parent 53d5233 commit 2feab2b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions object/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Site struct {
Tag string `xorm:"varchar(100)" json:"tag"`
Domain string `xorm:"varchar(100)" json:"domain"`
OtherDomains []string `xorm:"varchar(500)" json:"otherDomains"`
NeedRedirect bool `json:"needRedirect"`
Host string `xorm:"varchar(100)" json:"host"`
Port int `json:"port"`
SslMode string `xorm:"varchar(100)" json:"sslMode"`
Expand Down
8 changes: 6 additions & 2 deletions service/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func redirectToHttps(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, targetUrl, http.StatusMovedPermanently)
}

func redirectToNonWww(w http.ResponseWriter, r *http.Request, host string) {
func redirectToHost(w http.ResponseWriter, r *http.Request, host string) {
protocol := "https"
if r.TLS == nil {
protocol = "http"
Expand All @@ -90,9 +90,13 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
return
}

if site.Host != r.Host && site.NeedRedirect {
redirectToHost(w, r, site.Host)
}

hostNonWww := getHostNonWww(r.Host)
if hostNonWww != "" {
redirectToNonWww(w, r, hostNonWww)
redirectToHost(w, r, hostNonWww)
}

if site.Node == "" {
Expand Down
12 changes: 11 additions & 1 deletion web/src/SiteEditPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import React from "react";
import {Button, Card, Col, Input, InputNumber, Row, Select} from "antd";
import {Button, Card, Col, Input, InputNumber, Row, Select, Switch} from "antd";
import {LinkOutlined} from "@ant-design/icons";
import * as SiteBackend from "./backend/SiteBackend";
import * as CertBackend from "./backend/CertBackend";
Expand Down Expand Up @@ -146,6 +146,16 @@ class SiteEditPage extends React.Component {
</Select>
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={2}>
{i18next.t("site:Need redirect")}:
</Col>
<Col span={1} >
<Switch checked={this.state.site.needRedirect} onChange={checked => {
this.updateSiteField("needRedirect", checked);
}} />
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={2}>
{i18next.t("site:Host")}:
Expand Down
3 changes: 2 additions & 1 deletion web/src/SiteListPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class SiteListPage extends React.Component {
displayName: `New Site - ${randomName}`,
domain: "door.casdoor.com",
otherDomains: [],
needRedirect: false,
host: "",
port: 8000,
sslMode: "HTTP",
Expand Down Expand Up @@ -176,7 +177,7 @@ class SiteListPage extends React.Component {
return record.otherDomains.map(domain => {
return (
<a key={domain} target="_blank" rel="noreferrer" href={`https://${domain}`}>
<Tag color={"processing"}>
<Tag color={record.needRedirect ? "default" : "processing"}>
{domain}
</Tag>
</a>
Expand Down

0 comments on commit 2feab2b

Please sign in to comment.