├── LICENSE.txt ├── README.md ├── conf └── app.ini ├── linux_ip.go ├── main.go ├── middleware └── jwt.go ├── pkg ├── app │ └── response.go ├── logging │ ├── file.go │ └── log.go └── setting │ └── setting.go ├── qzcloud ├── definexml.go ├── paramstruct.go ├── qzcloud.go ├── qzcloudlib.go ├── setting.go └── tool.go ├── routers └── router.go ├── runtime └── logs │ ├── log20200711.log │ ├── log20200717.log │ ├── log20200806.log │ ├── log20200807.log │ ├── log20200809.log │ └── log20200822.log └── vnc ├── token └── token.conf └── utils ├── .eslintrc └── websockify ├── .gitignore ├── .gitmodules ├── .travis.yml ├── CHANGES.txt ├── LICENSE.txt ├── MANIFEST.in ├── Makefile ├── README.md ├── Windows ├── Windows Service Readme.txt └── noVNC Websocket Service Project │ ├── Program.cs │ ├── ProjectInstaller.Designer.cs │ ├── ProjectInstaller.cs │ ├── ProjectInstaller.resx │ ├── Properties │ └── AssemblyInfo.cs │ ├── Service1.Designer.cs │ ├── Service1.cs │ ├── noVNC Websocket.csproj │ └── noVNC Websocket.sln ├── include ├── VT100.js ├── keysym.js ├── util.js ├── websock.js ├── webutil.js ├── wsirc.js └── wstelnet.js ├── other ├── Makefile ├── README.md ├── js │ ├── README.md │ ├── package.json │ └── websockify.js ├── launch.sh ├── project.clj ├── websocket.c ├── websocket.h ├── websocket.rb ├── websockify.c ├── websockify.clj ├── websockify.rb └── wswrap ├── rebind ├── rebind.c ├── run ├── setup.py ├── test-requirements.txt ├── tox.ini ├── websockify.py ├── websockify ├── __init__.py ├── auth_plugins.py ├── sysloghandler.py ├── token_plugins.py ├── websocket.py ├── websocketproxy.py ├── websocketserver.py └── websockifyserver.py ├── wsirc.html └── wstelnet.html /LICENSE.txt: -------------------------------------------------------------------------------- 1 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 2 | 3 | 1. Definitions. 4 | 5 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 6 | 7 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 8 | 9 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 10 | 11 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 12 | 13 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 14 | 15 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 16 | 17 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 18 | 19 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 20 | 21 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 22 | 23 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 24 | 25 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 26 | 27 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 28 | 29 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 30 | 31 | You must give any other recipients of the Work or Derivative Works a copy of this License; and 32 | You must cause any modified files to carry prominent notices stating that You changed the files; and 33 | You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 34 | If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. 35 | 36 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 37 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 38 | 39 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 40 | 41 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 42 | 43 | 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 44 | 45 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 46 | 47 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | "# qzkvm" 2 | "# qzkvm" 3 | 4 | 轻舟云kvm云主机管理系统,受控完全开源,主控也是开源的,可在官网下载主控 5 | 开源代码仅供学习使用,不可作为商业产品, 6 | 轻舟云官网[www.qzsystem.com](http://www.qzsystem.com) 7 | 轻舟云 kvm+hyper-v全方位虚拟化系统提供商,香港 美国 韩国 大陆 一手机房服务器提供 8 | 9 | 10 | 11 | License 12 | qzkvm is licensed under the [Apache Licence, Version 2.0.](https://www.apache.org/licenses/LICENSE-2.0.html) -------------------------------------------------------------------------------- /conf/app.ini: -------------------------------------------------------------------------------- 1 | #debug or release 2 | RUN_MODE = debug 3 | 4 | [app] 5 | APIKEY = 123456 6 | 7 | [server] 8 | HTTP_PORT = 8000 9 | READ_TIMEOUT = 60 10 | WRITE_TIMEOUT = 60 11 | -------------------------------------------------------------------------------- /linux_ip.go: -------------------------------------------------------------------------------- 1 | /* 2 | Package guest_set_user_password - sync host<->guest communication 3 | 4 | Example: 5 | { "execute": "guest-get-cloud-init", "arguments": {"cmd":"cloud_kvm 192.168.10.2 192.168.10.1 255.255.255.0 8.8.8.8 8.8.8.6 "} } 6 | */ 7 | package main 8 | 9 | import ( 10 | "bufio" 11 | "fmt" 12 | "html/template" 13 | "io/ioutil" 14 | "net" 15 | "os" 16 | "os/exec" 17 | "regexp" 18 | "strconv" 19 | "strings" 20 | ) 21 | const ( 22 | centos = "/etc/sysconfig/network-scripts/ifcfg-" 23 | ubuntu = "/etc/network/interfaces" 24 | ubuntu1 = "/etc/network/interfaces.d/cloud.cfg" 25 | ubuntu2 = "/etc/netplan/50-cloud-init.yaml" 26 | centos_network_conf = `TYPE=Ethernet 27 | BOOTPROTO=static 28 | HWADDR={{.mac}} 29 | IPADDR={{.ip}} 30 | NETMASK={{.mask}} 31 | GATEWAY={{.gateway}} 32 | DNS1={{.dns1}} 33 | DNS2={{.dns2}} 34 | DEFROUTE=yes 35 | IPV4_FAILURE_FATAL=yes 36 | IPV6INIT=no 37 | NAME={{.name}} 38 | DEVICE={{.name}} 39 | ONBOOT=yes` 40 | centos_network_conf1 = `TYPE=Ethernet 41 | BOOTPROTO=static 42 | IPADDR={{.ip1}} 43 | NETMASK={{.mask1}} 44 | DEFROUTE=yes 45 | IPV4_FAILURE_FATAL=yes 46 | NAME={{.name1}} 47 | DEVICE={{.name1}} 48 | ONBOOT=yes` 49 | ubuntu1_network_conf = `# This file describes the network interfaces available on your system 50 | # and how to activate them. For more information, see interfaces(5). 51 | 52 | source /etc/network/interfaces.d/* 53 | # The loopback network interface 54 | auto lo 55 | iface lo inet loopback 56 | ` 57 | ubuntu2_network_conf1 = `auto {{.name}} 58 | iface {{.name}} inet static 59 | address {{.ip}} 60 | dns-nameservers {{.dns1}} {{.dns2}} 61 | netmask {{.mask}} 62 | gateway {{.gateway}} 63 | auto {{.name1}} 64 | iface {{.name1}} inet static 65 | address {{.ip1}} 66 | netmask {{.mask1}}` 67 | ubuntu2_network_conf2 = `network: 68 | renderer: networkd 69 | ethernets: 70 | {{.name}}: 71 | addresses: [{{.ip}}/{{.mask}}] 72 | gateway4: {{.gateway}} 73 | nameservers: 74 | addresses: [{{.dns1}}, {{.dns2}}] 75 | search: [] 76 | optional: true 77 | {{.name1}}: 78 | addresses: [{{.ip1}}/{{.mask1}}] 79 | optional: true 80 | version: 2` 81 | ) 82 | 83 | func main() { 84 | f, err := os.Open("/root/ip.txt") 85 | if err != nil { 86 | panic(err) 87 | } 88 | defer f.Close() 89 | ipInfo:=map[string]string{} 90 | scanner := bufio.NewScanner(f) 91 | os_,ver_:=OsAndVer() 92 | i:=0 93 | for scanner.Scan(){ 94 | i+=1 95 | line:=scanner.Text() //以'\n'为结束符读入一行 96 | line = strings.Replace(line,"\n","",1) 97 | line = strings.Trim(line,"") 98 | //ip|gateway|mask|dns1|dns2|mac 99 | /** 100 | 192.168.10.1.2|192.168.10.1|255.255.255.0|8.8.8.8|8.8.8.6|mac 101 | 192.168.10.1.2|192.168.10.1|255.255.255.0|eth1 102 | hostname 103 | */ 104 | ip:=strings.Split(line,"|") 105 | if i==1{ 106 | ipInfo["ip"] = ip[0] 107 | ipInfo["gateway"] = ip[1] 108 | ipInfo["mask"] = ip[2] 109 | ipInfo["dns1"] = ip[3] 110 | ipInfo["dns2"] = ip[4] 111 | ipInfo["mac"] = ip[5] 112 | }else if(i==2){ 113 | ipInfo["ip1"] = ip[0] 114 | ipInfo["gateway1"] = ip[1] 115 | ipInfo["mask1"] = ip[2] 116 | ipInfo["mac1"] = ip[3] 117 | }else{ 118 | SetHostName(os_,line) 119 | } 120 | 121 | } 122 | SetIp(ipInfo,os_,ver_) 123 | } 124 | func SetIp(ipInfo map[string]string, os_, ver string) { 125 | tag := InterfacesTag(ipInfo["mac"]) 126 | tag1 := InterfacesTag(ipInfo["mac1"]) 127 | ipInfo["name"] = tag 128 | ipInfo["name1"] = tag1 129 | if strings.Index(strings.ToLower(os_),"centos")>-1 || strings.Index(strings.ToLower(os_),"fedora")>-1 { 130 | os.Remove(centos+"eth0") 131 | path := centos + tag 132 | os.Remove(path) 133 | file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644) 134 | if err != nil { 135 | panic(err) 136 | } 137 | defer file.Close() 138 | t := template.New("ipset") 139 | t.Parse(centos_network_conf) 140 | t.Execute(file, ipInfo) 141 | 142 | os.Remove(centos+"eth1") 143 | path1:= centos + tag1 144 | file1, err := os.OpenFile(path1, os.O_WRONLY|os.O_CREATE, 0644) 145 | if err != nil { 146 | //panic(err) 147 | } 148 | defer file1.Close() 149 | t = template.New("ipset1") 150 | t.Parse(centos_network_conf1) 151 | t.Execute(file1, ipInfo) 152 | 153 | var ver1 float64 154 | if ver != "" { 155 | ver1, _ = strconv.ParseFloat(ver, 10) 156 | } 157 | if strings.ToLower(os_) == "fedora" { 158 | cmd := exec.Command("bash", "-c", "nmcli con reload;nmcli con reload;nmcli con up "+tag+";systemctl restart network") 159 | cmd.Run() 160 | } 161 | if ver1 >= 7 && ver1<8{ 162 | cmd := exec.Command("bash", "-c", "systemctl stop NetworkManager;systemctl disable NetworkManager") 163 | cmd.Run() 164 | 165 | com:=fmt.Sprintf("ifconfig %s down;ifconfig %s up;ifconfig %s down;ifconfig %s up",tag,tag,tag1,tag1) 166 | cmd = exec.Command("bash", "-c", com) 167 | cmd.Run() 168 | 169 | cmd = exec.Command("bash", "-c", "systemctl restart network") 170 | cmd.Run() 171 | 172 | }else if ver1 >= 8 && ver1<9{ 173 | cmd := exec.Command("bash", "-c", "systemctl start NetworkManager;systemctl enable NetworkManager") 174 | cmd.Run() 175 | 176 | com:=fmt.Sprintf("ifdown %s;ifup %s;ifdown %s ;ifup %s up",tag,tag,tag1,tag1) 177 | cmd = exec.Command("bash", "-c", com) 178 | cmd.Run() 179 | 180 | cmd = exec.Command("bash", "-c", "systemctl restart NetworkManager") 181 | cmd.Run() 182 | } else { 183 | cmd := exec.Command("bash", "-c", "service NetworkManager stop;chkconfig NetworkManager off;service network restart") 184 | cmd.Run() 185 | } 186 | } 187 | if strings.ToLower(os_) == "ubuntu" || strings.ToLower(os_) == "debian" { 188 | if ver != "" { 189 | ver2, _ := strconv.ParseFloat(ver, 10) 190 | if ver2 <= 17 || strings.ToLower(os_) == "debian" { 191 | ioutil.WriteFile(ubuntu, []byte(ubuntu1_network_conf), 0666) //写入文件(字节数组) 192 | file, err := os.OpenFile(ubuntu1, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) 193 | if err != nil { 194 | panic(err) 195 | } 196 | defer file.Close() 197 | t := template.New("ipset") 198 | t.Parse(ubuntu2_network_conf1) 199 | t.Execute(file, ipInfo) 200 | 201 | dns:=fmt.Sprintf(`echo nameserver %s > /etc/resolv.conf;echo nameserver %s >> /etc/resolv.conf`,ipInfo["dns1"],ipInfo["dns2"]) 202 | 203 | cmd := exec.Command("bash", "-c", dns) 204 | cmd.Run() 205 | cmd = exec.Command("bash", "-c", "/etc/init.d/networking restart") 206 | cmd.Run() 207 | } 208 | if ver2 > 17 { 209 | cmd1 := exec.Command("bash", "-c", "sudo rm -r /etc/netplan/*.ymal") 210 | cmd1.Run() 211 | file, err := os.OpenFile(ubuntu2, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) 212 | if err != nil { 213 | panic(err) 214 | } 215 | defer file.Close() 216 | t := template.New("ipset") 217 | t.Parse(ubuntu2_network_conf2) 218 | ipInfo["mask"] = MASK2CIDR(ipInfo["mask"]) 219 | t.Execute(file, ipInfo) 220 | cmd := exec.Command("bash", "-c", "sudo netplan apply;sudo service network-manager stop") 221 | cmd.Run() 222 | } 223 | } 224 | } 225 | } 226 | 227 | func SetHostName(os_, newHostName string) { 228 | if strings.ToLower(os_) == "centos" { 229 | network_, _ := ioutil.ReadFile("/etc/sysconfig/network") 230 | osRegexp_ := regexp.MustCompile("[^_]HOSTNAME=\"?(.*)\"?") 231 | hostName := osRegexp_.FindSubmatch(network_) 232 | if len(hostName)>0{ 233 | hostName_ := strings.Trim(string(hostName[1]), " ") 234 | hostName1_ := strings.Trim(string(hostName_), "\n") 235 | com1 := fmt.Sprintf(`sudo sed -i "s/%s/%s/g" /etc/sysconfig/network`, hostName1_, newHostName) 236 | com2 := fmt.Sprintf(`sudo sed -i "s/%s/%s/g" /etc/hosts`, hostName1_, newHostName) 237 | cmd1 := exec.Command("bash", "-c", com1+";"+com2+";hostname "+newHostName) 238 | cmd1.Run() 239 | }else{ 240 | hostName_, _ := ioutil.ReadFile("/etc/hostname") 241 | hostName1_ := strings.Trim(string(hostName_), "\n") 242 | hostName1_ = strings.Trim(string(hostName1_), " ") 243 | com1 := fmt.Sprintf(`sed -i "s/%s/%s/g" /etc/hostname`, hostName1_, newHostName) 244 | com2 := fmt.Sprintf(`sed -i "s/%s/%s/g" /etc/hosts`, hostName1_, newHostName) 245 | cmd1 := exec.Command("bash", "-c", com1+";"+com2+";hostname "+newHostName) 246 | cmd1.Run() 247 | } 248 | 249 | } else if strings.ToLower(os_) == "debian" { 250 | hostName_, _ := ioutil.ReadFile("/etc/hostname") 251 | hostName1_ := strings.Trim(string(hostName_), "\n") 252 | hostName1_ = strings.Trim(string(hostName1_), " ") 253 | com1 := fmt.Sprintf(`sed -i "s/%s/%s/g" /etc/hostname`, hostName1_, newHostName) 254 | com2 := fmt.Sprintf(`sed -i "s/%s/%s/g" /etc/hosts`, hostName1_, newHostName) 255 | cmd1 := exec.Command("bash", "-c", com1+";"+com2+";hostname "+newHostName) 256 | cmd1.Run() 257 | }else if strings.ToLower(os_) == "fedora" { 258 | hostName_, _ := ioutil.ReadFile("/etc/hostname") 259 | hostName1_ := strings.Trim(string(hostName_), "\n") 260 | hostName1_ = strings.Trim(string(hostName1_), " ") 261 | if len(strings.Trim(hostName1_,""))<2{ 262 | network_, _ := ioutil.ReadFile("/etc/sysconfig/network") 263 | osRegexp_ := regexp.MustCompile("[^_]HOSTNAME=\"?(.*)\"?") 264 | hostName := osRegexp_.FindSubmatch(network_) 265 | if len(hostName)>0{ 266 | hostName_ := strings.Trim(string(hostName[1]), " ") 267 | hostName1_ = strings.Trim(string(hostName_), "\n") 268 | } 269 | } 270 | com1 := fmt.Sprintf(`sed -i "s/%s/%s/g" /etc/hostname`, hostName1_, newHostName) 271 | com2 := fmt.Sprintf(`sed -i "s/%s/%s/g" /etc/hosts`, hostName1_, newHostName) 272 | cmd1 := exec.Command("bash", "-c", com1+";"+com2+";hostname "+newHostName) 273 | cmd1.Run() 274 | } else { 275 | hostName_, _ := ioutil.ReadFile("/etc/hostname") 276 | hostName1_ := strings.Trim(string(hostName_), "\n") 277 | hostName1_ = strings.Trim(string(hostName1_), " ") 278 | com1 := fmt.Sprintf(`sudo sed -i "s/%s/%s/g" /etc/hostname`, hostName1_, newHostName) 279 | com2 := fmt.Sprintf(`sudo sed -i "s/%s/%s/g" /etc/hosts`, hostName1_, newHostName) 280 | cmd1 := exec.Command("bash", "-c", com1+";"+com2+";sudo hostname "+newHostName) 281 | cmd1.Run() 282 | } 283 | } 284 | 285 | func OsAndVer() (string, string) { 286 | var osStr, verStr string 287 | if IsFileExist("/etc/os-release") { 288 | release, _ := ioutil.ReadFile("/etc/os-release") 289 | osRegexp := regexp.MustCompile("[^_]ID=\"?(.*)\"?") 290 | os_ := osRegexp.FindSubmatch(release) 291 | verRegexp := regexp.MustCompile("VERSION_ID=\"(.*)\"") 292 | ver := verRegexp.FindSubmatch(release) 293 | if len(os_) > 0 { 294 | osStr = string(os_[1]) 295 | } 296 | if len(ver) > 0 { 297 | verStr = string(ver[1]) 298 | } 299 | } else if IsFileExist("/etc/centos-release") { 300 | release, _ := ioutil.ReadFile("/etc/centos-release") 301 | osRegexp := regexp.MustCompile("(CentOS)") 302 | releaseInfo := osRegexp.FindSubmatch(release) 303 | osStr = string(releaseInfo[1]) 304 | } else { 305 | return "", "" 306 | } 307 | return strings.Trim(osStr,`"`),strings.Trim(verStr,`"`) 308 | } 309 | 310 | func InterfacesTag(name string) string { 311 | name = strings.Replace(name,"\n","",1) 312 | name = strings.Trim(name,"") 313 | tag, err := net.Interfaces() 314 | if err!=nil{ 315 | return "eth0" 316 | } 317 | for k, t := range tag { 318 | if k==0{ 319 | continue 320 | } 321 | if strings.EqualFold(t.HardwareAddr.String(),name){ 322 | return t.Name 323 | } 324 | if strings.EqualFold(t.Name,name){ 325 | return t.Name 326 | } 327 | } 328 | return "eth0" 329 | } 330 | 331 | func IsFileExist(filename string) bool { 332 | if _, err := os.Stat(filename); os.IsNotExist(err) { 333 | return false 334 | } 335 | return true 336 | } 337 | 338 | func MASK2CIDR(mask string) string { 339 | masks := strings.Split(mask, ".") 340 | mask1, _ := strconv.ParseInt(masks[0], 10, 64) 341 | mask2, _ := strconv.ParseInt(masks[1], 10, 64) 342 | mask3, _ := strconv.ParseInt(masks[2], 10, 64) 343 | mask4, _ := strconv.ParseInt(masks[3], 10, 64) 344 | ones, _ := net.IPv4Mask(byte(mask1), byte(mask2), byte(mask3), byte(mask4)).Size() 345 | return strconv.FormatInt(int64(ones), 10) 346 | } 347 | 348 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "qzkvm/src/pkg/logging" 7 | "qzkvm/src/pkg/setting" 8 | "qzkvm/src/qzcloud" 9 | "qzkvm/src/routers" 10 | ) 11 | 12 | func init() { 13 | qzcloud.Setup() 14 | } 15 | func main() { 16 | router := routers.InitRouter() 17 | 18 | s := &http.Server{ 19 | Addr: fmt.Sprintf(":%d", setting.HTTPPort), 20 | Handler: router, 21 | ReadTimeout: setting.ReadTimeout, 22 | WriteTimeout: setting.WriteTimeout, 23 | MaxHeaderBytes: 1 << 20, 24 | } 25 | logging.Info("[info] start http server listening %s", setting.HTTPPort) 26 | s.ListenAndServe() 27 | } -------------------------------------------------------------------------------- /middleware/jwt.go: -------------------------------------------------------------------------------- 1 | package jwt 2 | 3 | import ( 4 | "github.com/gin-gonic/gin" 5 | "net/http" 6 | "qzkvm/src/pkg/setting" 7 | "qzkvm/src/qzcloud" 8 | "strings" 9 | ) 10 | 11 | func JWT() gin.HandlerFunc { 12 | err:=qzcloud.ConnError 13 | return func(c *gin.Context) { 14 | var data interface{} 15 | if(strings.Trim(c.GetHeader("apikey"),"")!=""&&strings.Trim(c.GetHeader("apikey"),"")==strings.Trim(setting.APIKEY,"")){ 16 | if err!=nil{ 17 | c.JSON(http.StatusUnauthorized, gin.H{ 18 | "code" : 0, 19 | "msg" :err.Error(), 20 | "data" :"", 21 | }) 22 | 23 | c.Abort() 24 | return 25 | } 26 | c.Next() 27 | }else{ 28 | c.JSON(http.StatusUnauthorized, gin.H{ 29 | "code" : 0, 30 | "msg" :"apikey error", 31 | "data" : data, 32 | }) 33 | c.Abort() 34 | return 35 | } 36 | 37 | } 38 | } -------------------------------------------------------------------------------- /pkg/app/response.go: -------------------------------------------------------------------------------- 1 | package app 2 | 3 | import ( 4 | "github.com/gin-gonic/gin" 5 | "net/http" 6 | ) 7 | 8 | type Gin struct { 9 | C *gin.Context 10 | } 11 | 12 | type Response struct { 13 | Code int `json:"code"` 14 | Msg string `json:"msg"` 15 | Data interface{} `json:"data"` 16 | } 17 | 18 | // Response setting gin.JSON 19 | func (g *Gin) Response(errCode int, msg string,data interface{}) { 20 | g.C.JSON(http.StatusOK, Response{ 21 | Code: errCode, 22 | Msg: msg, 23 | Data: data, 24 | }) 25 | return 26 | } -------------------------------------------------------------------------------- /pkg/logging/file.go: -------------------------------------------------------------------------------- 1 | package logging 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "os" 7 | "time" 8 | ) 9 | 10 | var ( 11 | LogSavePath = "runtime/logs/" 12 | LogSaveName = "log" 13 | LogFileExt = "log" 14 | TimeFormat = "20060102" 15 | ) 16 | 17 | func getLogFilePath() string { 18 | return fmt.Sprintf("%s", LogSavePath) 19 | } 20 | 21 | func getLogFileFullPath() string { 22 | prefixPath := getLogFilePath() 23 | suffixPath := fmt.Sprintf("%s%s.%s", LogSaveName, time.Now().Format(TimeFormat), LogFileExt) 24 | 25 | return fmt.Sprintf("%s%s", prefixPath, suffixPath) 26 | } 27 | 28 | func openLogFile(filePath string) *os.File { 29 | _, err := os.Stat(filePath) 30 | switch { 31 | case os.IsNotExist(err): 32 | mkDir(getLogFilePath()) 33 | case os.IsPermission(err): 34 | log.Fatalf("Permission :%v", err) 35 | } 36 | 37 | handle, err := os.OpenFile(filePath, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644) 38 | if err != nil { 39 | log.Fatalf("Fail to OpenFile :%v", err) 40 | } 41 | 42 | return handle 43 | } 44 | 45 | func mkDir(filePath string) { 46 | dir, _ := os.Getwd() 47 | err := os.MkdirAll(dir + "/" + getLogFilePath(), os.ModePerm) 48 | if err != nil { 49 | panic(err) 50 | } 51 | } -------------------------------------------------------------------------------- /pkg/logging/log.go: -------------------------------------------------------------------------------- 1 | package logging 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "os" 7 | "path/filepath" 8 | "runtime" 9 | ) 10 | 11 | type Level int 12 | 13 | var ( 14 | F *os.File 15 | 16 | DefaultPrefix = "" 17 | DefaultCallerDepth = 2 18 | 19 | logger *log.Logger 20 | logPrefix = "" 21 | levelFlags = []string{"DEBUG", "INFO", "WARN", "ERROR", "FATAL"} 22 | ) 23 | 24 | const ( 25 | DEBUG Level = iota 26 | INFO 27 | WARNING 28 | ERROR 29 | FATAL 30 | ) 31 | 32 | func init() { 33 | filePath := getLogFileFullPath() 34 | F = openLogFile(filePath) 35 | 36 | logger = log.New(F, DefaultPrefix, log.LstdFlags) 37 | } 38 | 39 | func Debug(v ...interface{}) { 40 | setPrefix(DEBUG) 41 | logger.Println(v) 42 | } 43 | 44 | func Info(v ...interface{}) { 45 | setPrefix(INFO) 46 | logger.Println(v) 47 | } 48 | 49 | func Warn(v ...interface{}) { 50 | setPrefix(WARNING) 51 | logger.Println(v) 52 | } 53 | 54 | func Error(v ...interface{}) { 55 | setPrefix(ERROR) 56 | logger.Println(v) 57 | } 58 | 59 | func Fatal(v ...interface{}) { 60 | setPrefix(FATAL) 61 | logger.Fatalln(v) 62 | } 63 | 64 | func setPrefix(level Level) { 65 | _, file, line, ok := runtime.Caller(DefaultCallerDepth) 66 | if ok { 67 | logPrefix = fmt.Sprintf("[%s][%s:%d]", levelFlags[level], filepath.Base(file), line) 68 | } else { 69 | logPrefix = fmt.Sprintf("[%s]", levelFlags[level]) 70 | } 71 | 72 | logger.SetPrefix(logPrefix) 73 | } -------------------------------------------------------------------------------- /pkg/setting/setting.go: -------------------------------------------------------------------------------- 1 | package setting 2 | 3 | import ( 4 | "log" 5 | "time" 6 | 7 | "github.com/go-ini/ini" 8 | ) 9 | 10 | var ( 11 | Cfg *ini.File 12 | 13 | RunMode string 14 | 15 | HTTPPort int 16 | ReadTimeout time.Duration 17 | WriteTimeout time.Duration 18 | APIKEY string 19 | 20 | JwtSecret string 21 | ) 22 | 23 | func init() { 24 | var err error 25 | Cfg, err = ini.Load("conf/app.ini") 26 | if err != nil { 27 | log.Fatal(2, "Fail to parse 'conf/app.ini': %v", err) 28 | } 29 | 30 | LoadBase() 31 | LoadServer() 32 | LoadApp() 33 | } 34 | 35 | func LoadBase() { 36 | RunMode = Cfg.Section("").Key("RUN_MODE").MustString("debug") 37 | APIKEY = Cfg.Section("app").Key("APIKEY").MustString("") 38 | } 39 | 40 | func LoadServer() { 41 | sec, err := Cfg.GetSection("server") 42 | if err != nil { 43 | log.Fatal(2, "Fail to get section 'server': %v", err) 44 | } 45 | 46 | RunMode = Cfg.Section("").Key("RUN_MODE").MustString("debug") 47 | 48 | HTTPPort = sec.Key("HTTP_PORT").MustInt(8000) 49 | ReadTimeout = time.Duration(sec.Key("READ_TIMEOUT").MustInt(60)) * time.Second 50 | WriteTimeout = time.Duration(sec.Key("WRITE_TIMEOUT").MustInt(60)) * time.Second 51 | } 52 | 53 | func LoadApp() { 54 | sec, err := Cfg.GetSection("app") 55 | if err != nil { 56 | log.Fatal(2, "Fail to get section 'app': %v", err) 57 | } 58 | 59 | JwtSecret = sec.Key("JWT_SECRET").MustString("!@)*#)!@U#@*!@!)") 60 | } -------------------------------------------------------------------------------- /qzcloud/definexml.go: -------------------------------------------------------------------------------- 1 | package qzcloud 2 | 3 | func GetCreateDomainXml() string { 4 | xml := ` 5 | 6 | {{.host_name}} 7 | {{.host_uuid}} 8 | {{.max_ram}} 9 | {{.min_ram}} 10 | {{.cpu}} 11 | 12 | 13 | hvm 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | destroy 26 | restart 27 | destroy 28 | 29 | 30 | 31 | 32 | 33 | /usr/libexec/qemu-kvm 34 | 35 | 36 | 37 | 38 | 39 | 40 | {{.os_read}} 41 | {{.os_write}} 42 | {{.os_iops}} 43 | {{.os_iops}} 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | {{.data_read}} 52 | {{.data_write}} 53 | {{.data_iops}} 54 | {{.data_iops}} 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
64 | 65 | 66 |
67 | 68 | 69 | 70 |
71 | 72 | 73 | 74 |
75 | 76 | 77 | 78 |
79 | 80 | 81 | 82 |
83 | 84 | 85 |
86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | {{.filterip|unescaped}} 94 | 95 |
96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 |
106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 |
120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 |
129 | 130 |