Skip to content

Commit

Permalink
Feat: synctv init
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Oct 4, 2023
1 parent 586f37b commit acf7718
Show file tree
Hide file tree
Showing 48 changed files with 4,320 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/log
/public/dist/*
!*.gitkeep
.DS_Store
19 changes: 19 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[English](./README.md) | 中文

# 特点
- [x] 同步观看
- [x] 视频同步
- [x] 直播同步
- [x] 影院模式
- [x] 聊天
- [x] 弹幕
- [x] 代理
- [ ] 视频代理
- [ ] 直播代理

# 免责声明
- 这个程序是一个免费且开源的项目。它旨在播放网络上的视频文件,方便多人共同观看视频和学习golang。
- 在使用时,请遵守相关法律法规,不要滥用。
- 该程序仅进行客户端播放视频文件/流量转发,不会拦截、存储或篡改任何用户数据。
- 在使用该程序之前,您应该了解并承担相应的风险,包括但不限于版权纠纷、法律限制等,这与该程序无关。
- 如果有任何侵权行为,请通过[电子邮件](mailto:[email protected])与我联系,将及时处理。
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
# synctv
Synchronized viewing, theater, live streaming, video, long-distance relationship
English | [中文](./README-CN.md)

# Features
- [x] Synchronized viewing
- [x] Videos Sync
- [x] Live streaming
- [x] Theater
- [x] Chat
- [x] Bullet chat
- [x] Proxy
- [ ] Videos proxy
- [ ] Live proxy

# License
The `SyncTV` is open-source software licensed under the AGPL-3.0 license.

# Disclaimer
- This program is a free and open-source project. It aims to play video files on the internet, making it convenient for multiple people to watch videos and learn golang together.
- Please comply with relevant laws and regulations when using it, and do not abuse it.
- The program only plays video files/forwards traffic on the client-side and will not intercept, store, or tamper with any user data.
- Before using the program, you should understand and assume the corresponding risks, including but not limited to copyright disputes, legal restrictions, etc., which are not related to the program.
- If there is any infringement, please contact me via [email](mailto:[email protected]), and it will be dealt with promptly.
17 changes: 17 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd

import (
"github.com/gin-gonic/gin"
"github.com/synctv-org/synctv/cmd/flags"
)

func InitGinMode() error {
if flags.Dev {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
gin.ForceConsoleColor()

return nil
}
11 changes: 11 additions & 0 deletions cmd/conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package cmd

import "github.com/spf13/cobra"

func ConfCmd() *cobra.Command {
return &cobra.Command{
Use: "conf",
Short: "conf",
Long: `config file`,
}
}
16 changes: 16 additions & 0 deletions cmd/flags/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package flags

// Global
var (
Dev bool

LogStd bool

ConfigFile string

SkipEnv bool

SkipConfig bool

EnvNoPrefix bool
)
24 changes: 24 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"github.com/spf13/cobra"
"github.com/synctv-org/synctv/internal/bootstrap"
)

func Init(cmd *cobra.Command, args []string) error {
bootstrap.InitConfig()
bootstrap.InitLog()
bootstrap.InitSysNotify()
return nil
}

var InitCmd = &cobra.Command{
Use: "init",
Short: "init and check config",
Long: `auto create config file or check config, and auto add new key and delete old key`,
RunE: Init,
}

func init() {
RootCmd.AddCommand(InitCmd)
}
31 changes: 31 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
"github.com/synctv-org/synctv/cmd/flags"
)

var RootCmd = &cobra.Command{
Use: "synctv-server",
Short: "synctv-server",
Long: `synctv-server https://github.com/synctv-org/synctv`,
}

func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

func init() {
RootCmd.PersistentFlags().BoolVar(&flags.Dev, "dev", false, "start with dev mode")
RootCmd.PersistentFlags().BoolVar(&flags.LogStd, "log-std", true, "log to std")
RootCmd.PersistentFlags().BoolVar(&flags.EnvNoPrefix, "env-no-prefix", false, "env no SYNCTV_ prefix")
RootCmd.PersistentFlags().BoolVar(&flags.SkipConfig, "skip-config", false, "skip config")
RootCmd.PersistentFlags().BoolVar(&flags.SkipEnv, "skip-env", false, "skip env")
RootCmd.PersistentFlags().StringVarP(&flags.ConfigFile, "config", "f", "", "config file path")
}
113 changes: 113 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package cmd

import (
"fmt"
"net"
"net/http"

"github.com/quic-go/quic-go/http3"
log "github.com/sirupsen/logrus"
"github.com/soheilhy/cmux"
"github.com/spf13/cobra"
"github.com/synctv-org/synctv/internal/bootstrap"
"github.com/synctv-org/synctv/internal/conf"
"github.com/synctv-org/synctv/server"
)

var ServerCmd = &cobra.Command{
Use: "server",
Short: "Start synctv-server",
Long: `Start synctv-server`,
PersistentPreRunE: Init,
PreRunE: func(cmd *cobra.Command, args []string) error { return InitGinMode() },
Run: Server,
}

func Server(cmd *cobra.Command, args []string) {
tcpServerAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", conf.Conf.Server.Listen, conf.Conf.Server.Port))
if err != nil {
log.Panic(err)
}
udpServerAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", conf.Conf.Server.Listen, conf.Conf.Server.Port))
if err != nil {
log.Panic(err)
}
serverListener, err := net.ListenTCP("tcp", tcpServerAddr)
if err != nil {
log.Panic(err)
}
var useMux bool
if conf.Conf.Rtmp.Port == 0 || conf.Conf.Rtmp.Port == conf.Conf.Server.Port {
useMux = true
conf.Conf.Rtmp.Port = conf.Conf.Server.Port
}
tcpRtmpAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", conf.Conf.Server.Listen, conf.Conf.Rtmp.Port))
if err != nil {
log.Fatal(err)
}
if conf.Conf.Rtmp.Enable {
if useMux {
muxer := cmux.New(serverListener)
e, s := server.NewAndInit()
switch {
case conf.Conf.Server.CertPath != "" && conf.Conf.Server.KeyPath != "":
httpl := muxer.Match(cmux.HTTP2(), cmux.TLS())
go http.ServeTLS(httpl, e.Handler(), conf.Conf.Server.CertPath, conf.Conf.Server.KeyPath)
if conf.Conf.Server.Quic {
go http3.ListenAndServeQUIC(udpServerAddr.String(), conf.Conf.Server.CertPath, conf.Conf.Server.KeyPath, e.Handler())
}
case conf.Conf.Server.CertPath == "" && conf.Conf.Server.KeyPath == "":
httpl := muxer.Match(cmux.HTTP1Fast())
go e.RunListener(httpl)
default:
log.Panic("cert and key must be both set")
}
tcp := muxer.Match(cmux.Any())
go s.Serve(tcp)
go muxer.Serve()
} else {
e, s := server.NewAndInit()
switch {
case conf.Conf.Server.CertPath != "" && conf.Conf.Server.KeyPath != "":
go http.ServeTLS(serverListener, e.Handler(), conf.Conf.Server.CertPath, conf.Conf.Server.KeyPath)
if conf.Conf.Server.Quic {
go http3.ListenAndServeQUIC(udpServerAddr.String(), conf.Conf.Server.CertPath, conf.Conf.Server.KeyPath, e.Handler())
}
case conf.Conf.Server.CertPath == "" && conf.Conf.Server.KeyPath == "":
go e.RunListener(serverListener)
default:
log.Panic("cert and key must be both set")
}
rtmpListener, err := net.ListenTCP("tcp", tcpRtmpAddr)
if err != nil {
log.Fatal(err)
}
go s.Serve(rtmpListener)
}
} else {
e, _ := server.NewAndInit()
switch {
case conf.Conf.Server.CertPath != "" && conf.Conf.Server.KeyPath != "":
go http.ServeTLS(serverListener, e.Handler(), conf.Conf.Server.CertPath, conf.Conf.Server.KeyPath)
if conf.Conf.Server.Quic {
go http3.ListenAndServeQUIC(udpServerAddr.String(), conf.Conf.Server.CertPath, conf.Conf.Server.KeyPath, e.Handler())
}
case conf.Conf.Server.CertPath == "" && conf.Conf.Server.KeyPath == "":
go e.RunListener(serverListener)
default:
log.Panic("cert and key must be both set")
}
}
if conf.Conf.Rtmp.Enable {
log.Infof("rtmp run on tcp://%s:%d", tcpServerAddr.IP, tcpRtmpAddr.Port)
}
if conf.Conf.Server.Quic && conf.Conf.Server.CertPath != "" && conf.Conf.Server.KeyPath != "" {
log.Infof("quic run on udp://%s:%d", udpServerAddr.IP, udpServerAddr.Port)
}
log.Infof("website run on http://%s:%d", tcpServerAddr.IP, tcpServerAddr.Port)
bootstrap.WaitCbk()
}

func init() {
RootCmd.AddCommand(ServerCmd)
}
20 changes: 20 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var VersionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of Sync TV Server",
Long: `All software has versions. This is Sync TV Server's`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("synctv-server v0.1 -- HEAD")
},
}

func init() {
RootCmd.AddCommand(VersionCmd)
}
67 changes: 67 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module github.com/synctv-org/synctv

go 1.18

require (
github.com/caarlos0/env/v9 v9.0.0
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.9.1
github.com/go-resty/resty/v2 v2.8.0
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/google/uuid v1.3.1
github.com/gorilla/websocket v1.5.0
github.com/json-iterator/go v1.1.12
github.com/maruel/natural v1.1.0
github.com/mitchellh/go-homedir v1.1.0
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/quic-go/quic-go v0.39.0
github.com/sirupsen/logrus v1.9.3
github.com/soheilhy/cmux v0.1.5
github.com/spf13/cobra v1.7.0
github.com/zijiren233/gencontainer v0.0.0-20230930061950-82324a07eacb
github.com/zijiren233/go-colorable v0.0.0-20230930131441-997304c961cb
github.com/zijiren233/ksync v0.2.0
github.com/zijiren233/livelib v0.0.0-20230930062256-1d07bbddcefb
github.com/zijiren233/stream v0.5.1
github.com/zijiren233/yaml-comment v0.2.0
golang.org/x/crypto v0.13.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/bytedance/sonic v1.10.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/google/pprof v0.0.0-20230926050212-f7f687d19a98 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/onsi/ginkgo/v2 v2.12.1 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-20 v0.3.4 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
go.uber.org/mock v0.3.0 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
)
Loading

0 comments on commit acf7718

Please sign in to comment.