如何在JavaScript中保存未经授权的操作 [英] How to save unauthorised manipulation in a JavaScript

查看:93
本文介绍了如何在JavaScript中保存未经授权的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript / HTML5中编写了一个服务器 - 客户端应用,它应该允许客户端使用服务器端的Node.js实时通信/玩游戏。



我知道使用私有变量等。但是如何防止整个游戏引擎通过控制台api进行未经授权的访问?



如何以一种所有变量落入私有范围的方式编写它,并且一旦启动它们就可以独立运行而无需在全局范围内注册单个变量所以没有人可以搞砸游戏!



从我研究的结果来看,我可以做类似的事情。

  function Game(){
//所有声明在这里
//在这里开始一个逻辑
}

然后调用它

  new Game(); 

会做到吗?但有没有更好的方法来做同样的事情?

解决方案

您可以通过匿名函数运行JavaScript应用程序而无需注册任何单个变量:

 (function(){
//这里的局部变量。
})();

然而没有可靠的方式防止作弊:可以轻松分析您的代码,并创建虚假的AJAX请求。使用最新的浏览器,捕获代码非常容易。



有了getter和setter,任何人都可以有效地拦截你的功能。使用不推荐使用的 arguments.callee.caller 属性,攻击者可以读取函数调用的来源,有效地访问此答案顶部定义的闭包。 / p>

示例:

  var _alert = alert; 
window.alert = null;
Object.defineProperty(window,'alert',{
'value':function(m){
console.log('Intercepted。Function source:'+ arguments.callee.caller) ;
_alert.call(this,m);
}
});
(function(){
var localVar ='secret';
alert('嗨!');
})();


I wrote a server-client app in javascript/HTML5 its supposed to allow clients to communicate/play a game in realtime using Node.js on the server side .

I know the use of private variables and etc . But how to prevent the whole game engine from unauthorised access via console api ?

As in how to write it in such a way that all variables fall in a private scope and once initiated they run pretty much independently without registering a single variable in the global scope so that nobody can mess the Game up!

From what i have researched i can do something like

 function Game(){
   // All declarations here
   // Start a logic in here
 }

and then calling it

 new Game();

will do it ? but is there any better way to do the same ?

解决方案

You can run a JavaScript application without registering any single variable, via an anonymous function:

(function() {
    //local variables here.
})();

However, there is no reliable way to prevent cheating: One can easily analyse your code, and create fake AJAX requests. With the latest browsers, it's incredibly easy to capture your code.

With getters and setters, anyone can effectively intercept your functions. Using the deprecated arguments.callee.caller property, an attacker can read the source of the function call, effectively getting access to the closure as defined at the top of this answer.

Example:

var _alert = alert;
window.alert = null;
Object.defineProperty(window, 'alert', {
    'value': function(m) {
        console.log('Intercepted. Function source: ' + arguments.callee.caller);
        _alert.call(this, m);
    }
});
(function(){
    var localVar = 'secret';
    alert('Hi!');
})();

这篇关于如何在JavaScript中保存未经授权的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆