Browse Source

Started support of OpenVPN

Dmitry Yu Okunev 3 years ago
parent
commit
574ecd5a14

+ 14 - 0
app/assets/stylesheets/vpn.scss

@@ -1,3 +1,17 @@
 // Place all the styles related to the vpn controller here.
 // They will automatically be included in application.css.
 // You can use Sass (SCSS) here: http://sass-lang.com/
+
+#accounts tr td {
+	padding:	0 15px 0 15px;
+	border:		none;
+	border-top:	1px #888 dotted;
+	border-left:	1px #888 dotted;
+}
+#accounts tr td:first-child {
+	border-left:	none;
+}
+#accounts tr td a {
+	border:		none
+}
+

+ 14 - 2
app/controllers/vpn_controller.rb

@@ -1,7 +1,19 @@
 class VpnController < ApplicationController
 	def openvpn
 		return if @user.nil?
-		@accounts = Account.find_by(:owner => @user["user"])
-		@accounts = Array.new if @accounts.nil?
+		@accounts = Account.where(:owner => @user["user"])
+	end
+
+	def get_openvpn_config
+		@account = Account.where(:login => params[:login].downcase, :owner => @user["user"]).first
+
+		if @account.nil?
+			render(:file => File.join(Rails.root, 'public/403.html'), :status => 403, :layout => false)
+		end
+
+		respond_to do |format|
+			format.zip { send_data @account.GetOpenVPNConfigZip,   :type => 'application/zip' }
+			format.gz  { send_data @account.GetOpenVPNConfigTarGz, :type => 'application/gzip' }
+		end
 	end
 end

+ 16 - 3
app/models/account.rb

@@ -2,21 +2,34 @@ class Account < ApplicationRecord
 	def self.GetFreeIPAddr
 		ipaddr_busy = Hash.new
 
-		Account.all.each do |account|
+		Account.find_each do |account|
 			ipaddr_busy[account.ipaddr] = true
 		end
 
 		require 'ipaddr'
 
-		first = IPAddr.new("192.168.127.0").to_i
+		first = IPAddr.new("192.168.127.2").to_i	# .0 - net; .1 - server, .2 - client, .3 - broadcast
 		last  = IPAddr.new("192.168.255.254").to_i
 
 		cur = first
 		while not ipaddr_busy[cur].nil? do
-			cur += 1
+			cur += 4	# netmask /30: next network == +4
 			throw :noFreeIPAddresses
 		end
 
 		return cur
 	end
+
+	def IPAddr
+		require 'ipaddr'
+		return IPAddr.new(self.ipaddr, Socket::AF_INET).to_s
+	end
+
+	def GetOpenVPNConfigZip
+		return `sudo openvpn-getconfig zip #{Shellwords.escape(self.login)}`
+	end
+
+	def GetOpenVPNConfigTarGz
+		return `sudo openvpn-getconfig tar.gz #{Shellwords.escape(self.login)}`
+	end
 end

File diff suppressed because it is too large
+ 24 - 3
app/views/vpn/openvpn.html.erb


+ 0 - 0
app/views/vpn/openvpn_debian.html.erb


+ 0 - 0
app/views/vpn/openvpn_fedora.html.erb


+ 0 - 0
app/views/vpn/openvpn_freebsd.html.erb


+ 0 - 0
app/views/vpn/openvpn_macos.html.erb


+ 0 - 0
app/views/vpn/openvpn_windows.html.erb


+ 2 - 0
config/initializers/mime_types.rb

@@ -4,4 +4,6 @@
 # Mime::Type.register "text/richtext", :rtf
 
 Mime::Type.register "image/jpeg", :jpeg
+Mime::Type.register "application/zip", :zip
+Mime::Type.register "application/gzip", :gz
 

+ 6 - 0
config/routes.rb

@@ -3,6 +3,12 @@ Rails.application.routes.draw do
 	get 'vpn/windows'
 	get 'vpn/macos'
 	get 'vpn/openvpn'
+	get 'vpn/openvpn-debian'
+	get 'vpn/openvpn-fedora'
+	get 'vpn/openvpn-freebsd'
+	get 'vpn/openvpn-windows'
+	get 'vpn/openvpn-macos'
+	get 'vpn/openvpn/:login', to: 'vpn#get_openvpn_config'
 
 	root 'vpn#about'
 	# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

+ 5 - 5
db/migrate/20161111073417_create_accounts.rb

@@ -1,14 +1,14 @@
 class CreateAccounts < ActiveRecord::Migration[5.0]
 	def change
 		create_table :accounts do |t|
-			t.integer :ipaddr, :unsigned => true, :limit => 8
-			t.integer :pulled
-			t.boolean :isused
-			t.string :login
+			t.bigint :ipaddr, :null => false
+			t.integer :pulled, :null => false
+			t.boolean :isused, :null => false
+			t.string :login, :null => false
 			t.string :password
 			t.integer :issue
 			t.string :comment
-			t.string :owner
+			t.string :owner, :null => false
 
 			t.timestamps
 

+ 5 - 5
db/schema.rb

@@ -13,14 +13,14 @@
 ActiveRecord::Schema.define(version: 20161111073417) do
 
   create_table "accounts", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
-    t.bigint   "ipaddr",                  unsigned: true
-    t.integer  "pulled"
-    t.boolean  "isused"
-    t.string   "login"
+    t.bigint   "ipaddr",     null: false
+    t.integer  "pulled",     null: false
+    t.boolean  "isused",     null: false
+    t.string   "login",      null: false
     t.string   "password"
     t.integer  "issue"
     t.string   "comment"
-    t.string   "owner"
+    t.string   "owner",      null: false
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
     t.index ["created_at"], name: "index_accounts_on_created_at", using: :btree